Sum of Digits Example C++ Program

Sum of Digits Definition:

Here modulus operator is used to add the digits of the input integer.

Sum of Digits Example Program

/*## Sum of Digits is odd or even In C++*/
/*## Calculation C++ Programs, Datatype C++ Programs, Basic C++ Programs*/

#include <iostream>
#include<conio.h>
#include<stdlib.h>

using namespace std;

int main() {
   // Declare Variables
   int inputNumber, temp, total = 0, remainder;

   cout << "Simple C++ Program : Sum of Digits\n";
   cout << "\nEnter a number whose sum is to be found : ";
   cin>>inputNumber;

   temp = inputNumber;

   while (temp != 0) {
      remainder = temp % 10;
      total = total + remainder;
      temp = temp / 10;
   }

   cout << "\nSum of digits of the number : " << inputNumber << " = " << total;

   getch();
   return (0);
}

Sample Output:

Simple C++ Program : Sum of Digits

Enter a number whose sum is to be found : 3421

Sum of digits of the number : 3421 = 10