Created
May 8, 2020 05:46
-
-
Save arrbxr/17b4bb5959540cd6998ca1d8e05af07d to your computer and use it in GitHub Desktop.
Sum of Digits Using Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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