I am working in Microsoft Visual C++ 2008.
Here is my sum of digits code:
#include <iostream>
using namespace std;
int main() {
int num;
int sum=0;
cout << "Number, please? ";
cin >> num;
while (num>0) {
sum=sum+num%10;
num=(num-num%10)/10;
}
cout << "Sum of digits: " << sum << endl;
return 0;
}
It works, the issue is that the program needs to work for an integer of any size. INT, LONG INT, UNSIGNED INT, not good enough. How does one find the sum of digits of a number without being able to store it in the first place?
What I was thinking is somehow inputting each digit as the user types it with cin.get() but my attempts at putting it in some type of loop have failed. NB: <iostream> and <iomanip> libraries only; that restriction keeps me from storing the number as a string and working from there.
I would like only the most general advice/hints please.
Thanks in advance.