help fast

I am new to C++ and i want use 2301 and 3302 but make 2301=6,(2+3+0+1) and 3302 =8(3+3+0+2) in a while loop how do i do that. but i also have 1234567890 how can add them?
Why don't you just say you want to calculate the digit sum of an integer?

There are basically two ways to go about this:
1) Convert the integer to a string, then each digit of the string to an integer. Then add those values up.
2) Extract the digit values with modulo and integer division
example:
30624 div 10000 = 3 (digit 1)
30624 mod 10000 = 624 (continue with that)

624 div 1000 = 0 (digit 2)
624 mod 1000 = 624 (continue with that)

624 div 100 = 6 (digit 3)
624 mod 100 = 24 (continue with that)

24 div 10 = 2 (digit 4)
24 mod 10 = 4 (continue with that)

digit 5 is now obviously 4.

Hint: Do this in a loop.
@ hanst99: I saw this post earlier and my problem is that the math doesn't work out. For Example: The factors for 2301 are 3^1, 13^1 and 59^1, how did you figure out what the OP is trying to do?
Last edited on
The OP is trying to calculate the digit sum, not to perform prime factorization...
And it would be more advantageous to extract the digits from the other end. That way, only division by 10 is needed, no matter how big the number.
cool i get the idea but now how to i do all the div and mod if the numbers are from a file. do i use a special variable like say fristNumber(30624) div 1000 =??(digit1)...... then add digit1+digit2 etc.
The division operator is / and modulo is %.
As for reading from files:
http://www.cplusplus.com/doc/tutorial/files/
Topic archived. No new replies allowed.