Write a program that reads an integer between 0 and 1000 and sum up all digits in the integer.for example if an integer is 932, the sum of the digits is 14.
#include <iostream>
using namespace std;
int main(){
cout<<"Input an integer: ";
int x; cin>>x;
if(x >= 0 && x <= 1000){
cout<<"Sum of digits in x is: ";
int sum = 0;
for(int i = 10; x > 0;){
sum += x % i;
x /= 10;
}
cout<<sum;
}
}
Comments
Leave a comment