Friday, September 23, 2011

WAP to find factorial of a given number using recursion


/*
  Name: WAP to find factorial of a given number using recursion
  Author: Parveen Malik
  Date: 23/09/11 10:25
*/


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


long factorial(int);


main()
{
   int num;
   long f;

   printf("Enter a number to find its factorial :");
   scanf("%d",&num); 

   if(num<0)
      printf("Negative numbers are not allowed");
   else
   {
      f = factorial(num);
      printf("%d!=%ld",num,f);
   }
   getch();
   return(0);
}

long factorial(int n)
{
   if(n==0)
      return(1);
   else
      return(n*factorial(n-1));
}

OUTPUT:


0 comments:

Post a Comment