I found an interesting problem regarding the 'digital root' of a number. In it you add the digits of a number until you find a single digit. For example, digital root of 1032 would be 1+0+3+2=6 and digital root of 2354 would be 2+3+5+4=14=1+4=5.
I made a recersive code about it but I can't arrive at a single digit. It gives you the sum of all the digits of the number but not the sum till a single digit. Here's the code:
Code:
#include<stdio.h>
int rem=0;
int digit_adder(int in)
{
int newnum;
rem = rem+(in%10);
newnum=in/10;
if(newnum==0)
{
return rem;
}
else
return digit_adder(newnum);
}
int main()
{
int no;
printf ("Enter the number: \n");
scanf ("%d", &no);
int ans=0;
printf("\n%d\n\n",(ans = digit_adder(no)));
return 0;
}
How can I change it into a code that gives me the root till a single digit?
Thanks.