Need help for repetition method!
Hi guys,
I have a question to ask:
Write a program to read in a positive integer and add up its digits repeatedly until the sum is a single digit. For example, if the integer is 12345, then adding its digits (1 + 2 + 3 + 4 + 5) yields 15, and adding its digits again (1 + 5) yields 6. Hence the answer is 6.
The tricky part for me is the part where the user can type in any digits he wishes.
From the angle i see it, I attempted to do smth along this line:
while x > 1 // x is the number the user entered
x = x/10
count++
using the count i know exactly how many digits are there, from then i proceed to identify each digit,
x % pow(10,n) * 10 to isolate each digit due to the correlation
my question is how to i assign the values i looped out to another variable? like digit1 = 2, digit 2 =2 in this looping function?
thanks for ur help!
|