Experiment no.
: 1 Date of Experiment :04-02-2023
Name : Ashutosh kumar chaubey Admission No.: 22SCSE1012384
Enrollment No. :22131012234
AIM : Write a C++ program to find the sum of individual digits of a positive
number.
Introduction : We can perform the sum of digits program in C++ language by the
help of loop and mathematical operation only. The main logic is extract the each
digit from last and find the sum and to extract the digit from the number just divide
it by 10. The remainder is the digit.
Algorithm :
o Step 1: Get number by user
o Step 2: Get the modulus/remainder of the number
o Step 3: sum the remainder of the number
o Step 4: Divide the number by 10
o Step 5: Repeat the step 2 while number is greater than 0.
Source code:
#include<iostream>
using namespace std;
int main()
{
int n,sum=0,m;
cout<<"Enter a number: ";
cin>>n;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
cout<<"Sum is= "<<sum;
return 0;
}
Output :