Navigation Bar

Programe to calculate sum of digits using recursion

#include<iostream>
using namespace std;
int calsum(int num);

int main() {
   int num, val;
   cout<<"Enter a number: ";
   cin>>num;
   val = calsum(num);
   cout<<"\nSum of the digits of "<<num<<" is "<<val;
   return 0;
}
int calsum(int num)
 {
   if (num != 0)
   {
      return num%10 + calsum(num/10);
   }
}
Output 

No comments:

Post a Comment