Monday, 30 June 2014

Digital Root

The digital root (also called repeated digital sum) of a number is a single digit value obtained by an iterative process of summing digits. Digital sum of 65536 is 7, because 6+5+5+3+6=25 and 2+5 = 7. Write a program that takes an integer as input and prints its digital root.

INPUT:
A single integer N

OUTPUT:
Digital root of the number N.

CONSTRAINTS:
1 <= N <= 10^7

Solution:

#include<stdio.h>
int main()
{
int N, sum=0;
scanf("%d",&N);
while(N>9){
sum=0;
while(N>0){
sum += N%10;
N = N/10;
}
N = sum;
}
printf("%d",N);
return 0;
}

No comments: