c++ problem from a book I'm working through
I've been trying to teach myself C++ through a book I picked up but this problem has me stumped ( its not homework):
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6; output the individual digits of 8030 as 8 0 3 0; output the individual digits of -2345 as 2 3 4 5.
heres the code I have but it separates the digits out in reverse order:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num, n;
int sum = 0;
cout<<"Enter a number: "<<flush;
cin>>num;
while (num != 0)
{
n = num % 10;
num = num / 10;
sum = sum + n;
cout<<setw(2)<<n;
}
cout<<endl;
cout<<"The sum is "<<sum<<endl;
return 0;
}