Write a C++ Program to find the Sum of Digits in a Number using a While loop with an example. Within the while loop, it divides the number into individual digits and then finds the sum of it.
Here, we used a cout statement to show you the value of the remainder and sum value in iteration wise which helps you can understand the execution.
#include<iostream>
using namespace std;
int main()
{
int number, reminder, digitSum = 0;
cout << "Please Enter the Number to calculate Sum of Digits = ";
cin >> number;
while (number > 0)
{
reminder = number % 10;
digitSum += reminder;
number = number / 10;
cout << "\nDigit = " << reminder << " and the Digit Sum = " << digitSum;
}
cout << "\n\nThe Sum of all Digits in a given Number = " << digitSum;
return 0;
}

C++ Program to find Sum of Digits in a Number using a For Loop
#include<iostream>
using namespace std;
int main()
{
int number, reminder, digitSum;
cout << "Please Enter the Number to calculate Sum of Digits = ";
cin >> number;
for(digitSum = 0; number > 0; number = number / 10)
{
reminder = number % 10;
digitSum += reminder;
cout << "\nDigit = " << reminder << " and the Digit Sum = " << digitSum;
}
cout << "\nThe Sum of all Digits in a given Number = " << digitSum;
return 0;
}
Please Enter the Number to calculate Sum of Digits = 785469
Digit = 9 and the Digit Sum = 9
Digit = 6 and the Digit Sum = 15
Digit = 4 and the Digit Sum = 19
Digit = 5 and the Digit Sum = 24
Digit = 8 and the Digit Sum = 32
Digit = 7 and the Digit Sum = 39
The Sum of all Digits in a given Number = 39
Program to find Sum of Digits in a Number using functions
In this C++ example, we separated the logic using functions.
#include<iostream>
using namespace std;
int sumOfDigits(int number)
{
int reminder, digitSum;
for(digitSum = 0; number > 0; number = number / 10)
{
reminder = number % 10;
digitSum += reminder;
cout << "\nDigit = " << reminder << " and the Digit Sum = " << digitSum;
}
return digitSum;
}
int main()
{
int num, digiSum;
cout << "Please Enter the Number to calculate Sum of Digits = ";
cin >> num;
digiSum = sumOfDigits(num);
cout << "\nThe Sum of all Digits in a given Number = " << digiSum;
return 0;
}
Please Enter the Number to calculate Sum of Digits = 78932
Digit = 2 and the Digit Sum = 2
Digit = 3 and the Digit Sum = 5
Digit = 9 and the Digit Sum = 14
Digit = 8 and the Digit Sum = 22
Digit = 7 and the Digit Sum = 29
The Sum of all Digits in a given Number = 29
The below Program will find the Sum of Digits in a Number using Recursion.
#include<iostream>
using namespace std;
int sumOfDigits(int number)
{
static int reminder, digitSum = 0;
if(number > 0)
{
reminder = number % 10;
digitSum += reminder;
sumOfDigits(number / 10);
return digitSum;
}
else
return 0;
}
int main()
{
int num, digiSum;
cout << "Please Enter the Number to calculate Sum of Digits = ";
cin >> num;
digiSum = sumOfDigits(num);
cout << "\nThe Sum of all Digits in a given Number = " << digiSum;
return 0;
}
Please Enter the Number to calculate Sum of Digits = 248769
The Sum of all Digits in a given Number = 36