Here is source code of the C++ program which gets a number from input and displays the sum of the digits in the given number. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
* C++ program to Display the Sum of the digits of a given Number
using namespace std;
int val, num, sum = 0;
cout << "Enter the number : ";
cin >> val;
num = val;
while (num != 0)
sum = sum + num % 10;
num = num / 10;
cout << "The sum of the digits of "
<< val << " is " << sum;
$ g++ main.cpp $ ./a.out Enter the number : 12345 The sum of the digits of 12345 is 15
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.