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

Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2024 Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz