Instructions
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
8030 as 8 0 3 0
2345526 as 2 3 4 5 5 2 6
4000 as 4 0 0 0
-2345 as 2 3 4 5
My code:
#include
using namespace std;
int main(int argc, char* argv[]){
// Write your main here
int a=0,b[100]={0},n=0,sum=0;
cin>>a;
while(a>0)
{
b[n]=a%10;
a/=10;
n++;
}
for(int i=n; i>0; i--)
{
printf("%d ",b[i-1]);
sum+=b[i-1];
}
printf(" Sum: %d ",sum);
cin.get(); cin.get();
return 0;
}
Problem:
The input needs to be -2345
and the output should result in
Sum: 0
but it's wrong,
the result of this code is 50%
and I don't know why, pls help