Problem Description
Write a program to find the sum of digits in a number using recursion.
Input and Output Format:
Input consists of a nonnegative integer.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.CODING ARENA
#include <stdio.h>
int sum(int a);
int main()
{
int n,r;
scanf("%d",&n);
r=sum(n);
printf("The sum of digits in %d is %d",n,r);
return 0;
}
int sum(int num)
{
if(num!=0)
{
return(num%10+sum(num/10));
}
else
{
return 0;
}
}
Test Case 1
Input (stdin)43
Expected OutputThe sum of digits in 43 is 7
Test Case 2
Input (stdin)84
Expected OutputThe sum of digits in 84 is 12
No comments:
Post a Comment