1# Factorial of a number using recursion
2
3def recur_factorial(n):
4 if n == 1:
5 return n
6 else:
7 return n*recur_factorial(n-1)
8
9num = 7
10
11# check if the number is negative
12if num < 0:
13 print("Sorry, factorial does not exist for negative numbers")
14elif num == 0:
15 print("The factorial of 0 is 1")
16else:
17 print("The factorial of", num, "is", recur_factorial(num))
18
1//using recursion to find factorial of a number
2
3#include<stdio.h>
4
5int fact(int n);
6
7int main()
8{
9 int n;
10 printf("Enter the number: ");
11 scanf("%d",&n);
12
13 printf("Factorial of %d = %d", n, fact(n));
14
15}
16
17int fact(int n)
18
19{
20 if (n>=1)
21 return n*fact(n-1);
22 else
23 return 1;
24}
1FUNCTION FACTORIAL (N: INTEGER): INTEGER
2(* RECURSIVE COMPUTATION OF N FACTORIAL *)
3
4BEGIN
5 (* TEST FOR STOPPING STATE *)
6 IF N <= 0 THEN
7 FACTORIAL := 1
8 ELSE
9 FACTORIAL := N * FACTORIAL(N - 1)
10END; (* FACTORIAL *)
11
1#include <stdio.h>
2int factorial(int number){
3 if(number==1){
4 return number;
5 }
6 return number*factorial(number - 1);
7}
8int main(){
9 int a=factorial(5);
10 printf("%d",a);
11}
12
1// Factorial of n = 1*2*3*...*n
2
3#include <iostream>
4using namespace std;
5
6int factorial(int);
7
8int main() {
9 int n, result;
10
11 cout << "Enter a non-negative number: ";
12 cin >> n;
13
14 result = factorial(n);
15 cout << "Factorial of " << n << " = " << result;
16 return 0;
17}
18
19int factorial(int n) {
20 if (n > 1) {
21 return n * factorial(n - 1);
22 } else {
23 return 1;
24 }
25}