Write a program to find the sum of digits of a 5 digit number using recursion.

 Code:-


#include<stdio.h>

//function for find sum of the digits
int sum=0;
int Sum_digit(int n)
{
int r;
if(n==0)
return sum;
else
{
r=n%10;
sum=sum+r;
n=n/10;
Sum_digit(n);
}
}

//Driver function
int main()
{
int n;
printf(“Enter a numbern”);
scanf(“%d”,&n);
printf(“Sum of digits are : %d”,Sum_digit(n));
return 0;
}

Output:-

Enter a number
5
Sum of the digits are: 120



Leave a Reply

Your email address will not be published.