write a C-program to calculate the sum of digits of a number with recursion

 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:





NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

1 Comments

Post a Comment
Previous Post Next Post