Sum of Digits means Sum of Digits !!
123 = 1+2+3
111 = 1+1+1
Before moving to program, Let’s see a algorithm to understand better what we are going to
Steps
1. Get Number by User/ or Already defined it
2. Get the remainder/modulus of that number
3. Sum the remainder of the number
4. Divide the number of 10 to get the quotient
5. Repeat the step 2 until number is greater than 0.
Program to find out sum of digits in c++ language
· #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<<endl;
· return 0;
· }
Ouput –
Enter a number: 62
Sum is= 8
For Video Tutorials Subscribe our Youtube Channel
---------------------------------------------------------------------------------------------------------------------------
Previous Page Next Page
Any Doubt ? Feel free to write a comment
Join us on LinkedIn – Great Place of Networking
Follow us on More Together – Facebook for regular updates
Mandatory to join Telegram group for regular job updates
Mandatory to join whatsapp group for regular job updates
Other C++ Programs
For Other Tutorials – Click Here