Header Ads Widget

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 number\n");
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



Post a Comment

0 Comments