# C++ Program to Find Sum of Digits of a Number

In this article, you will learn and get code on finding the sum of digits of a given number by user at run-time using C++ programming. The same program is created with following approaches:

• Find Sum of Digits of a Number using while Loop
• using for Loop
• without using Loop
• using Function
• using Class

To add digits of any number in C++ programming, you have to ask from user to enter the number to add its digits and display the addition result on output screen as shown here in the following program.

## Using while Loop

Let's first start while loop. That is, the program given below find and prints the sum of digits of a number using while loop in C++.

```#include<iostream>
using namespace std;
int main()
{
int num, rem, sum=0;
cout<<"Enter the Number: ";
cin>>num;
while(num>0)
{
rem = num%10;
sum = sum+rem;
num = num/10;
}
cout<<"\nSum of Digits = "<<sum;
cout<<endl;
return 0;
}```

This program was build and run under Code::Blocks IDE. Here is its sample run:

Now supply any number as input say 1234 and press `ENTER` key to see the summation result of its digit. The sum of digits of a number 1234 evaluates to be 1+2+3+4 or 10 as shown in the output given below:

The dry run of above program goes like:

• After receiving the number say 1234, num=1234
• Because it is greater than 0, therefore condition evaluates to be true, and program flow goes inside the while loop
• num%10 or 1234%10 or 4 gets initialized to rem. So rem=4
• sum+rem or 0+4 (initial value of sum is 0) or 4 gets initialized to sum
• The new value of sum is 4
• num/10 or 1234/10 or 123 gets initialized to num
• Again it is greater than 0, therefore the condition of while loop evaluates to be true, and again the program flow goes inside the loop
• num%10 or 123%10 or 3 gets initialized to rem. So rem=3
• sum+rem or 4+3 or 7 gets initialized to sum
• Now the new value of sum is 7
• num/10 or 123/10 or 12 gets initialized to num
• Again it is greater than 0, therefore program flow again goes inside the loop, and executes all the three statements
• Continue the operation, until the value of num becomes 0
• After exiting from the loop, we'll have a variable sum that holds the sum of digits of given number
• Therefore, just print the value of sum as output

## Using for Loop

The same program is created here using for loop. Unlike while loop, the initialization and update part can also be written under for loop. Therefore, we have initialized the value of sum=0 as first statement of for loop, the second statement is condition checking as done in while loop, and at last, the update part is written as the third statement under for loop. Rest of the things will be same

```#include<iostream>
using namespace std;
int main()
{
int num, rem, sum;
cout<<"Enter the Number: ";
cin>>num;
for(sum=0; num>0; num=num/10)
{
rem = num%10;
sum = sum+rem;
}
cout<<"\nSum of Digits = "<<sum;
cout<<endl;
return 0;
}```

This program will produce the same output as of previous one.

## Without using Loop

Let's create the same program without using any type of loop. This program uses goto keyword available in C++ language, that sends the program flow at desired position inside the program. To execute this, we have to create a label say CODESCRSCKER and use the name of label to send the program flow at that label provided after the keyword goto.

```#include<iostream>
using namespace std;
int main()
{
int num, rem, sum=0;
cout<<"Enter the Number: ";
cin>>num;
CODESCRSCKER:
rem = num%10;
sum = sum+rem;
num = num/10;
if(num>0)
goto CODESCRSCKER;
cout<<"\nSum of Digits = "<<sum;
cout<<endl;
return 0;
}```

All the following three statements (comes under CODESCRSCKER label):

```rem = num%10;
sum = sum+rem;
num = num/10;```

gets executed at first. And using if block, checks the condition, whether the value of num is greater than 0 or not. If it evaluates to be true, then using goto keyword program flow goes to the label named CODESCRSCKER and again executes the same three statements. The process continues, until the value of num becomes 0

## Using user-defined Function

This program also does the same job as of all previous programs have done. The only difference is, this program finds sum of digits of number using a user-defined function named findSumOfDigit().

```#include<iostream>
using namespace std;
int findSumOfDigit(int);
int main()
{
int num;
cout<<"Enter the Number: ";
cin>>num;
cout<<"\nSum of Digits = "<<findSumOfDigit(num);
cout<<endl;
return 0;
}
int findSumOfDigit(int num)
{
int sum=0, rem;
while(num>0)
{
rem = num%10;
sum = sum+rem;
num = num/10;
}
return sum;
}```

The function findSumOfDigit() takes the given number as its argument and returns the sum of digits of its argument, that is the number entered by user at run-time.

## Using Class

This is the last program that uses class to find sum of digits of a number in C++.

```#include<iostream>
using namespace std;
class CodesCracker
{
private:
int num, sum, rem;
public:
void getNumber();
int findSumOfDigit();
};
void CodesCracker::getNumber()
{
cout<<"Enter the Number: ";
cin>>num;
}
int CodesCracker::findSumOfDigit()
{
sum=0;
while(num>0)
{
rem = num%10;
sum = sum+rem;
num = num/10;
}
return sum;
}
int main()
{
CodesCracker c;
int sum;
c.getNumber();
sum = c.findSumOfDigit();
cout<<"\nSum of Digits = "<<sum;
cout<<endl;
return 0;
}```

To learn about class in C++, you can follow the separate tutorial on it.

#### Same Program in Other Languages

C++ Online Test

« Previous Program Next Program »

Like/Share Us on Facebook 😋