Wednesday, 28 November 2012

Determine Factorial value of a number using recursive function in C program

Write this program in Turbo C++ or Dev C++ and Run it.


#include<stdio.h>
#include<conio.h>
int factorial(int x);
void main(){
       int n;
       printf("Enter Number:");
       scanf("%d",&n);
       if(n<0){
         printf("This is nagetive number!");
       }
       else{
         printf("Factorial of %d is %d",n,factorial(n));
       }
       getch();
}
int factorial(int x){
    int fact=1;
    if(x>=1){
       fact=x*factorial(x-1);
    }
    
 return (fact);   
}

Output:

Enter Number:6
Factorial of 6 is 720


No comments:

Post a Comment