Pages

Monday, November 16, 2015

C Program for sum of digits using Recursion

Program

#include<stdio.h>
int findsum(int);
void main()
{
  int num,x;
  printf("\nEnter a number: ");
  scanf("%d",&num);
  x=findsum(num);
  printf("Sum of the digits of %d is: %d",num,x);

}

int r,s;
int findsum(int n)
{
if(n)
{
r=n%10;
s=s+r;
findsum(n/10);
}
 else
return s;
}

Output

Enter a number: 235
Sum of the digits of 235 is: 10

No comments:

Post a Comment