Friday, April 6, 2012

SUM OF DIGITS OF A NUMBER USING RECURSION


#include<stdio.h> 
#include<conio.h>

void main()
{
  int num,x;
  clrscr();
  printf("\nEnter a number: ");
  scanf("%d",&num);
  x=findsum(num);
  printf("Sum of the digits of %d is: %d",num,x);
  getch();
}
  int r,s;
  int findsum(int n)
  {
            if(n)
            {
                        r=n%10;
                        s=s+r;
                        findsum(n/10);
            }
            else
                        return s;
  }

No comments:

Post a Comment