Q:

Write a program to enter a number between 1 to 100 and print the sum of the digits

0

Write a program to enter a number between 1 to 100 and print the sum of the digits

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;
int main(){
   int num;
int sum = 0;
N: cout << " Enter a number : ";
cin >> num;
if(num>0 && num<1000){
    while ( num > 0 ){
        sum += num % 10;
        num /= 10;
    }
cout << "Sum = " << sum;
}
else{
    cout<<"error";
    goto N;
}
}

 

Output:

Enter a number : 25
Sum = 7

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Find even and odd numbers using c++...