computing should continue until summation of individual digits of a number less than 10.

given big number should be added one after another until it sum of output reach less than 10.
input say 456789
i should manipulate in code like
1.4+5+6+7+8+9=39
2.check 39<10 if not again 3+9=11
3.check 11<10 if not again 1+1=2
4.check 2<10 if yes


I can guess i should right a recursive function.

but i done have an idea to how to break an individual integer into pieces.

Any help??
I can guess i should right a recursive function.
You can use iterative function too. Recursion is not mandatory here.

but i done have an idea to how to break an individual integer into pieces.
There are two ways:
1) convert integer to string, and treat each character as integer.
2) Find number modulo 10, this is last digit. Divide (with integer division) number by 10, this will remove last digit from number. Repeat while number is not 0.


BUT, as a mathematician I cannot miss an opportunity to apply some interesting properties of sum of sum ... of digits: it is equal to ((number - 1) % 9) + 1.
Thanks for your response miinipaa..!!

Convert integer to string we can do. also we can store it in string array.
Using iteration is it possible to sum charecter..?? it will not throw syntax error.??

//2) Find number modulo 10, this is last digit. Divide (with integer division) number by 10, this will remove last digit from number. Repeat while number is not 0.//

No i want to add individual digits only.

anyhow thanks for your other info how it can be handled mathemetically.
No i want to add individual digits only.
THis will be individual digits in the end:
1
2
3
4
5
6
7
8
9
10
1234 % 10 = 4 ← first digit
1234 / 10 = 123 //get rid of digit
123 % 10 = 3 ← second digit
123 / 10 =12
12 % 10 = 2 ← third digit
12 / 10 = 1
1 % 10 = 1 ← fourth digit
1 / 10 = 0 //STOP

Add extracted digits: 4 + 3 + 2 + 1


Using iteration is it possible to sum charecter
Convert digit character to numbers first. For ASCII platforms it is simple:
1
2
char digit = '5';
int number  = digit - '0'
Topic archived. No new replies allowed.