Monday, June 18, 2012

To find the factorial of a number using recursion


/* Program to find the factorial of a number using recursion */

#include<stdio.h>

#include<conio.h>

int findFactorial(int n);

void main()

{

  int m,fact;

  clrscr();

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

  scanf("%d",&m);

  fact=findFactorial(m);

  printf("\n Factorial of %d is %d",m,fact);

  getch();

}

 

int findFactorial(int n)

{

  int f;

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

    return 1;

  else

    f=n*findFactorial(n-1);             /* Recursive Call */

  return(f);

}

 

OUTPUT

Enter a number:5

 

 Factorial of 5 is 120

 

No comments:

Post a Comment

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