Home >>Cpp Programs >Sum Of Digits Program in C++

Sum Of Digits Program in C++

Sum Of Digits Program in C++

The sum of digits program in C++ is generally used to obtain the sum a number's digits that it possesses. For instance; the sum of digits of 30 will be 3 + 0 = 3. Writing this program is very simple in C++ programming language as it requires only the help of mathematical operation and the loop only.

Sum of digits algorithm

In order to retrieve the sum of two digits number you have to follow the below depicted algorithm:

  • Receive the number that is entered by the user.
  • Second step involves getting the modulus/remainder of the received number.
  • Then the sum of the remainder of the number is done.
  • In the next step, the number is divided by 10.
  • Now, the step 2 will be repeated till the provided number is greater than 0.

Here is the program of the sum of the digits that will give you deep understanding of the topic and make you understand that how these things work:

#include <iostream>  
using namespace std;  
int main()  
{  
int num,sum=0,rem,count;    
cout<<"Enter a number: ";    
cin>>num;    
while(num>0)    
{    
rem=num%10;    
sum=sum+rem;    
num=num/10;
count++;    
}    
cout<<"Sum is of given number = "<<sum<<endl;
cout<<"Total Digit of given number = "<<count<<endl;    
return 0;  
}
Output :
Enter a number: 12345
Sum is of given number = 15
Total Digit of given number = 5

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++
No Sidebar ads