Monday, June 18, 2012

To find the sum of the digits of a number using recursion


/* Program to find the sum of the digits of a number using recursion */

#include<stdio.h>

#include<conio.h>

int findSum(int n);

void main()

{

  int m,s;

  clrscr();

  printf("\n Enter the number:");

  scanf("%d",&m);

  s=findSum(m);

  printf("\nSum of the number %d is %d",m,s);

  getch();

}

 

int findSum(int n)

{

  int sum;

  if(n==0)           /* Stopping Condition */

    return 0;

  else

    sum=findSum(n/10)+(n%10);              /* Recursive Call */

  return (sum);

}

 

OUTPUT

 

 Enter the number:2364

 

Sum of the number 2364 is 15

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.