Question #225891

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.


Expert's answer

#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;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS