Digital Roots
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 50643 accepted Submission (s): 15810
Problem Description The digital root of a positive integer is found by summing the digits of the. If The resulting value is a single digit then this digit is the digital root. If The resulting value contains two or more digits, those digits, are summed and the ' process is repeated. This is continued as long as necessary to obtain a single digit.
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a-digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since isn't 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.
Input the input file would contain a list of positive integers, one per line. The end of the input would be indicated by the integer value of zero.
Output for each integer in the input, output it digital root on a separate line of the output.
Sample Input
24 39 0
Sample Output
6 3
Nine method: The number of a number is equal to this number modulo 9, also equal to all digits of the sum modulo 9, 9 of the words directly output
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
string S;
while (1)
{
cin>>s;
if (s== "0")
Break
int n=0;
for (int i=0;i<s.size (); i++)
{
n + = (s[i]-' 0 ');
n%= 9;
}
if (n==0)
cout<<9<<endl;
Else
cout<<n<<endl;
}
return 0;
}