Friday, March 7, 2014

WRITE A FUNCTION TO CALCULATE THE SUM OF DIGITS OF A NUMBER USING RECURSION

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int sum(int);
int n,s;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
s=sum(n);
printf("the sum of digits of %d is %d",n,s);
getch();
}

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

No comments:

Post a Comment