Sum of digits of a Number using C++ Language

Program code:

#include<iostream>
using namespace std;

int main()
{
int num, mod , rem ,sum = 0;
    char redo;
do
{
sum = 0;

cout<<"Enter any number =  ";
cin>> num ;
while(num)
{
mod =  num % 10 ;
num =  num / 10 ;

sum = sum + mod  ;
}
cout<<"\n\nSum of Digits of a Number is =  " << sum ;
cout<< endl<<endl;
        cout<<"Enter y for next calculation =  ";
cin>>redo;
}while(redo == 'y');
return 0;
}

Output:



No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.