Wednesday, 13 July 2016

WRITE A RECURSIVE C FUNCTION TO CALCULATE THE SUM OF DIGITS OF A NUMBER. USE THIS FUNCTION IN MAIN TO ACCEPT A NUMBER AND PRINT SUM OF ITS DIGIT

#include<stdio.h>
int sum(int);
main()
{
int n,c;
printf("Enter the no");
scanf("%d",&n);
c=sum(n);
printf("%d",c);
}
int sum(int n)
{
int r,d;
if(n==0)
return 0;
else
{
r=n%10;
d=r+sum(n/10);
return d;
}

}



ASSIGNMENT 8)

  TO DEMONSTRATE RECURSION

No comments:

Post a Comment