-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFactorial.c
More file actions
41 lines (35 loc) · 679 Bytes
/
Factorial.c
File metadata and controls
41 lines (35 loc) · 679 Bytes
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
33
34
35
36
37
38
39
40
41
/*Program to find the factorial of a number by recursive and iterative methods*/
#include<stdio.h>
#include<stdlib.h>
long int fact(int n);
long int Ifact(int n);
int main( )
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
if(num<0) {
printf("No factorial for negative number\n");
exit(1);
}
printf("Factorial of %d is %ld\n", num, fact(num) );
printf("Factorial of %d is %ld\n", num, Ifact(num) );
}/*End of main()*/
/*Recursive*/
long int fact(int n)
{
if(n == 0)
return(1);
return(n * fact(n-1));
}/*End of fact()*/
/*Iterative*/
long int Ifact(int n)
{
long fact=1;
while(n>0)
{
fact = fact*n;
n--;
}
return fact;
}/*End of ifact()*/