Sum of digits (recursive)
Send Feedback
Write a recursive function that returns the sum of the digits of a given integer.
Input format :
Output format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
Integer N
Sum of digits of N
0 <= N <= 10^9
12345
15
9
9
SOLUTION:-
#include <iostream>
using namespace std;
int sumOfDigits(int n) {
// Write your code here
if(n<10)
{
if(n==0)
{
return 0;
}
else
return n;
}
int d=n%10;
return d+sumOfDigits(n/10);
}
int main() { int n; cin >> n; cout << sumOfDigits(n) << endl; }
This is dummy text. It is not meant to be read. Accordingly, it is difficult to figure out when to end it. But then, this is dummy text. It is not meant to be read. Period.
2 comments
Click here for commentsGreat🙌
ReplyThanks buddy
ReplyIf you have any doubts then please let me know... ConversionConversion EmoticonEmoticon