Programming Examples
Cpp program to accept a integer number and find its sum of digit
Write a program to accept a integer number from user and find its sum of digit.
Sample 1:
Input : 123
Output : sum of Digit : 6
Sample 2:
Input : 1234
Output : Sum of Digit : 10
#include<iostream>
using namespace std;
int main(){
int num,digit,sum=0;
cout<<"Enter any integer number";
cin>>num;
while(num>0) {
digit=num%10; //get the last digit of the number
sum=sum+digit; //add the last digit to the sum
num=num/10; //remove the last digit from the number
}
cout<<"Sum is:"<<sum;
return 0;
}
Output
Enter any integer number 456
Sum is:15