Monday, June 25, 2012

Factorial using Recursion

Program to calculate Factorial using Recursion.

A
1: #include<stdio.h>  
2: int factorial(int);  
3: int main() {  
4:     int n,f;  
5:     printf("Enter number you want ot calculate factorial\n");  
6:     scanf("%d",&n);  
7:     f = factorial(n);  
8:     printf("Factorial of %d is = %d",n,f);  
9:     return(0);  
10: }  
11: int factorial(int f) {  
12:     int fact;  
13:     if(f <= 1) {  
14:        return(1);  
15:     }  
16:     else {  
17:       fact = (f*(factorial(f-1)));  
18:       return(fact);  
19:     }  
20: }  

Output :- 

1:  Enter number you want to calculate factorial 5  
2:  Factorial of 5 is = 120  


Post a Comment