TASK-
write a C-program to calculate the sum of digits of a number with recursion.
CODE:
#include<stdio.h>
int digit_sum(int y);
int main(){
int num,ret;
printf("Enter a number: ");
scanf("%d",&num); //taking number from user
ret=digit_sum(num); //calling function
printf("\nSum of digit of number is %d",ret);
getchar();
return 0;
}
// defining function
int digit_sum(int y)
{
static int dig,sum=0;
if (y>0)
{
dig=y%10;//last digit of number
sum=sum+dig; // sum of digits one by one
y/=10;
digit_sum(y);//applying recursion
}
return sum;
}
EXPECTED OUTPUT:
GREAT
ReplyDelete