codescracker
c++

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



« Previous Program Next Program »


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:

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++.

// Find Sum of Digits of a Number using while Loop
// ----codescracker.com----

#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:

C++ program add number digits

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:

find sum of digits of number c++

The dry run of above program goes like:

Using for Loop

The same program is created here using for loop. Unlike while loop, the initialization and updation 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 updatation part is written as the third statement under for loop. Rest of the things will be same

// Find Sum of Digits of a Number using for Loop
// ----codescracker.com----

#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.

// Find Sum of Digits of a Number without using Loop
// ----codescracker.com----

#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().

// Sum of Digits of a Number using Function
// ----codescracker.com----

#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++.

// Sum of Digits of a Number using Class
// ----codescracker.com----

#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


« Previous Program Next Program »