Write a C++ program to find the sum of digits of given integer number.
Program Code:
#include<iostream>
using namespace std;
int main()
{
int number;
int temp;
int sum=0;
int reminder;
cout<<"Enter any number to find the sum of their digits: ";
cin>>number;
temp =number;
// you can also use while(temp >0) in place of for(;temp>0;) loop
for(;temp > 0;)
{
reminder = temp % 10;
temp =temp / 10;
sum = sum + reminder;
}
cout<<"Sum of digits of given number is : ";
cout<<sum;
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter any number to find the sum of their digits: 334
Sum of digits of given number is : 10
Thanks
Mukesh Rajput
Post A Comment:
0 comments: