Skip to content

Commit c1ed3b1

Browse files
neddstarkkthuva4
authored andcommitted
Added Factorial in C using recursion and in python without recursion (thuva4#628)
* Added Factorial in C using recursion * Added factorial program in python
1 parent a3aca36 commit c1ed3b1

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

C/Factorial/Factorial.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<stdio.h>
2+
3+
long factorial(int);
4+
5+
int main()
6+
{
7+
int n;
8+
long f;
9+
10+
printf("Enter an integer to find its factorial\n");
11+
scanf("%d", &n);
12+
13+
if (n < 0)
14+
printf("Factorial of negative integers isn't defined.\n");
15+
else
16+
{
17+
f = factorial(n);
18+
printf("%d! = %ld\n", n, f);
19+
}
20+
21+
return 0;
22+
}
23+
24+
long factorial(int n)
25+
{
26+
if (n == 0)
27+
return 1;
28+
else
29+
return(n * factorial(n-1));
30+
}

Python/Factorial/factorial.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
number = int(input("Enter the number whose factorial you want: "))
2+
if number < 0:
3+
print("Factorial of negative numbers cannot be computed!")
4+
5+
product = 1
6+
for i in range(1, number+1):
7+
product = product*i
8+
9+
10+
print(str(number) + "! = " + str(product))

0 commit comments

Comments
 (0)