Skip to content

Instantly share code, notes, and snippets.

@arrbxr
Created May 8, 2020 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrbxr/17b4bb5959540cd6998ca1d8e05af07d to your computer and use it in GitHub Desktop.
Save arrbxr/17b4bb5959540cd6998ca1d8e05af07d to your computer and use it in GitHub Desktop.
Sum of Digits Using Recursion
// Sum of Digits Using Recursion
#include <iostream>
using namespace std;
int DigitSum(int);
int main() {
int num, sum;
cout<<"Enter Number to add digit: ";
cin>>num;
sum = DigitSum(num);
cout<<"Sum = "<<sum<<endl;
}
int DigitSum(int n) {
if(n == 0) return 0;
else
return ((n%10) + DigitSum(n / 10));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment