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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: