Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 48262 Accepted Submission(s): 15000
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
24 39 0
6 3
#include<stdio.h>
#include<string.h>
int fun(int p)
{
int n=0;
while(p>9)
{
n+=p%10;
p=p/10;
}
n+=p;
return n;
}
int main()
{
char p[1010];
int i,a;
while(gets(p)&&p[0]!='0')
{
a=0;
for(i=0;i<strlen(p);i++)
{
a+=p[i]-'0';
}
while(a>9)
{
{a=fun (a);}
}
printf("%d\n",a);
}
return 0;
}