# Need help for repetition method!

Discussion in 'C' started by rangerys, Sep 8, 2010.

1. ### rangerysNew Member

Joined:
Sep 8, 2010
Messages:
1
0
Trophy Points:
0
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!

Joined:
Dec 20, 2009
Messages:
572