Friday, October 30, 2009

Sum of 5 digits using recursion

A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5-digit number Using recursion

code obtained from: https://www.blogger.com/comment.g?blogID=20225658&postID=115134493576072577 -->comment by (rahul sahay)

void main()
{

int f,l;
printf("\nInput a 5 digit number");
scanf("%d",&f);
l=sum(f);
printf("\nThe sum of the digits is %d",l);
}

sum(int n)
{
if(n==0)
return 0;
else
return(n%10+sum(n/10));
}

10 comments: