C++ Program to Find Sum of Digits of A Number Using Classes

Have you ever wondered that how to find sum of digits in C++ using classes?

It is a quite easy job. I will tell you step-by step that how you can manage to find sum of digits of an integer in C++.

Digit sum means adding all the digits of a particular number.

For example, if we take a number 532 then its sum of digits will be:

5+3+2=10

Program Logic & Structure:

If we think about the logic behind summation of digits of a number then we can conclude that:

  1. Take a number from user as input.
  2. Grab each digit from that number.
  3. Add all the digits grabbed from number.
  4. Display the sum on screen.

One question arises that how we will extract the digits from given number?

The answer to this question is very simple. Dividing a number by 10 gives us the last digit of that number as remainder. We will continue a loop to grab each digit of the number and sum them up.

 Recommended reading: C++ Program For Addition of Two Numbers Using Classes

C++ Code For Sum of Digits of Number Using Classes:

#include<iostream>
using namespace std;	
class SUMdigits
{
	private:
		int num,sum;
 	public:
 		SUMdigits()
 		{
 			num=sum=0;
		}
    void input()
    {
      cout<<"ENTER NUMBER: ";
      cin>>num;
    }
    void sum_Function()
    {
      while(num>0)
      {
        int rem;
        rem=num%10;
        sum=sum+rem;
        num=num/10;
      }
    }
    void output()
    {
     cout<<"SUM OF DIGITS: "<<sum;
    }
};
int main()
{
    SUMdigits obj;
    obj.input();
    obj.sum_Function();
    obj.output();
    return 0;
}

Code Explanation:

This program is based on the concepts of Object Oriented Programming (OOP). As data members, classes, member functions are used in this program to accomplish the task.

First of all, we defined a new class named SUMdigits and included two data members i.e. variables named num & sum.

We also defined a constructor which initializes the values of both variables to zero.

Next, we declared a function to get input from user. We asked the user to enter a number and stored it in num variable.

After that, we defined a function named digits_Sum i.e. main part of this class. This function extracts all the digits from number and sum them.

Here, we declared rem variable to store the remainder of division.

We initialized a loop which continues until num  is greater than 0. It means that loop will stop if there is no more digit in given number.

 Recommended reading: C++ Project For Address Book Management

I have told you before that dividing a number by 10 gives us the last digit of that number as remainder. It means that one digit is extracted from number and we stored that digit in rem variable.

Next, we added this number to sum variable which is the result of this program.

At the end, we used output() function to show the sum of digits on screen.

In main function, we simple declared an object and used all three functions of SUMdigits class.

Output:

Sum of digits of number - Output

If you want to learn about finding sum of digits of number without classes then you can Read Here.

If you liked this article, then I would really appreciate if you follow us on Twitter and Facebook.

If you have any question then you can ask me in comments. I would like to answer them.

You can also Click Here To Download Exercise Files.