Answer to Question #225891 in C++ for Ulando

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.


1
Expert's answer
2021-08-17T12:55:51-0400
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS