Tuesday, 10 January 2012

Factorial of a given number using Recursion

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

long factorial(int);

void main()
{
    int num;
    long f;
    printf("ENTER A NUMBER TO FIND FACTORIAL :");
    scanf("%d",&num);
    if(num<0)
    printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
    else
    {
        f = factorial(num);
        printf("%d!=%ld",num,f);
    }
    getch();
}

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


Output of code:

factorial code output

No comments:

Post a Comment