C++ Program to obtain sum of digits of a given number using recursion [DEVCPP/GCC]

PROGRAM

#include <iostream>

using namespace std;

int sumOfDigits(int n)
{
        if(n==0)
        {
              return 0;
        }
        else
        {
              return (n%10) + sumOfDigits(n/10);
        }
}

int main()
{
        int num;

        cout<<"\nENTER NUMBER: ";
        cin>>num;

        cout<<"\nSUM OF DIGITS: "<<sumOfDigits(num);

        return 0;
}

OUTPUT

C++ Program to obtain sum of digits of a given number using recursion with output

Share this

Related Posts

1 comments :

comments
November 5, 2020 at 3:57 AM delete

The program will not work in case of 10 and more digits

Reply
avatar

FIND US ON FACEBOOK!