Programming Examples
Recursive program in c to find the sum of digit
Write a recursive function in to Find the Sum of Digit
Solution
#include<stdio.h>
int sum_of_digit(int);
int main()
{
int num,sod;
printf("Enter any Number");
scanf("%d",&num);
sod=sum_of_digit(num);
printf("Sum of Digit : %d",sod);
return 0;
}
int sum_of_digit(int n)
{
if(n==0)
{
return (0);
}
else
{
return (n%10+sum_of_digit(n/10));
}
}
Output
Enter any Number124
Sum of Digit : 7