View Single Post
Old 09-14-2004, 07:52 AM   #2 (permalink)
welshbyte
Insane
 
Location: Wales, UK, Europe, Earth, Milky Way, Universe
I've knocked together a little solution which works with int's but i'm afraid 8790278573 doesnt fit in an int (on my computer at least). You can see the method i used anyway:

Code:
#include<iostream>

using namespace std;

int main() {

	int i = 790278573;  // Number you want to add the digits of

	int icopy = i;
	int result = 0;
        
	while(icopy > 0) {
		result += icopy % 10;
		icopy /= 10;
	}

	cout << "Initial number: " << i      << "\n";
	cout << "Sum of digits : " << result << "\n";
}
I'd like to see a better method if anyone would like to post one. I'm not that hot on C++

Hope this helps anyway
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte
welshbyte is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62