C++ Program to Add Digits of a Number using Recursion

Write C++ Program to Add Digits of a Number using Recursion

// CPP Program to Add Digits of a Number using Recursion

#include <iostream>

using namespace std;

int add_digit(long int n)
{
	int sum = 0;
	if (n == 0)
	{
		return n;
	}
	sum = add_digit(n / 10) + (n % 10);
    return sum;


}
int main()
{
	long int n;
	cout << "Enter Number :--> ";
	cin >> n;

	int result = add_digit(n);

	cout << "The sum of the digits is : " << result;
	return 0;
}

Output:

Enter Number :--> 654321
The sum of the digits is : 21

≪ Previous | Next ≫