C LAB‎ > ‎(Sem1) Introduction to C‎ > ‎

WAP to find the factorial of a number using recursive function.

posted Nov 6, 2010, 2:29 AM by Neil Mathew   [ updated Nov 6, 2010, 2:39 AM ]

SOURCE CODE:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
 
int fact(int f)
{
 
if( f==1 || f==0)
{ return 1; }
else
{
f=f*fact(f-1);
return f;
}
 
}
 
 
main()
{
int num; 
int factorial;
 
printf(" Enter the number whose factorial needs to be found: ");
scanf(" %d ", &num);
 
factorial=fact(num);
 
printf("\n The factorial of %d is %d. ",num,factorial);
 
}
 
 
 

OUTPUT:

 Enter the number whose factorial needs to be found: 0

 The factorial of 0 is 1. 
 Enter the number whose factorial needs to be found: 4

 The factorial of 4 is 24. 


Comments