How to Find Sum of Digits of a Number using Recursion in C++

Hello Learners,

In this session, we will get familiar with Recursion/Recursive Function. This program deals with two functions. One is the main and another one is the recursive function. This program is so beginner-friendly and I have used the variable relating to the exact meaning of it. The program is to find the sum of the digits of any number provided by the user using the Recursive function. We can also solve it by an iterative approach i.e. using loops. So, let’s start.

C++ Program to find Sum of Digits of a number using recursive function

To achieve proper understanding, the first and foremost question that arises is What is a Recursive Function?
When the function is called within the same function, it is known as recursion in C++. In simple words, a function which calls itself is known as a Recursive Function.

It is an alternative method to the iterative approach. Actually, what we do in loops is we keep repeating a block of command mentioned inside the loop and in recursion, we do the same thing. We just keep calling it again and again so that it works as a loop.

Now, in the program, we first have to make a recursive function named recc. It matches with the actual meaning of it. Then inside that function, we need to write the actual logic of the program. So, firstly we will separate the digits of the number and then will we keep adding it to get the desired output. We already know that normally numbers are in decimal form so if we divide and modulo it by 10, we could get all the digits in separate form.

For the above, we will set a variable i.e. rem. It will hold remainder and it should be an array so that it could contain all the digits in different indices. The remainder is nothing but different digits of the number. Then, we will divide the number by 10 and put it in the same variable i.e. n. Following it, we will create an empty variable sum and will keep adding all the remainder into it so that every digit will get added.

At last, we will call this function a number of times according to the condition that the number is greater than zero. Also, the value of the index controlling the remainder is increment by 1 each time we call.

In main, we will only ask the user to enter any number and then will call the function recc.


Let’s see the source code for a better understanding:

#include<iostream>
using namespace std;

int sum=0,rem[10];

int recc(int,int);
int recc(int n,int i)
{
        rem[i]=n%10;
  n=n/10;
  sum+=rem[i];
  if(n>0)
  recc(n,i+1);
  return sum;
}

int main()
{
  int num;
  cout<<"Enter any number: ";
  cin>>num;
  cout<<"Sum of digits: "<<recc(num,0);
  
  return 0;
}

Here, we initialize sum globally because we don’t want it to become zero every time we call the function.

Now, Let’s have a look on the output screen:

Enter any number: 6789
Sum of digits: 30

Enter any number: 67
Sum of digits: 13

I hope it was easy enough to understand. Please see my other blog posts as well which can be get by just clicking on my name mentioned below the main topic. Feel free to give me suggestions and ask any queries in the comment section.

Thank You..!

Regards,
Isha Rani Prasad
Codespeedy Tech Pvt Ltd.

Leave a Reply

Your email address will not be published. Required fields are marked *