#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
// C++ program to find factorial of given number
#include <iostream>
using namespace std;
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver code
int main()
{
int num = 5;
cout << "Factorial of "
<< num << " is " << factorial(num) << endl;
return 0;
}
// This code is contributed by Shivi_Aggarwal
# Factorial of a number using recursion
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
// Java program to find factorial of given number
class Test {
// method to find factorial of given number
static int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num
+ " is " + factorial(5));
}
}
// Java program to find factorial of given number
class Test {
// method to find factorial of given number
static int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num
+ " is " + factorial(5));
}
}
// C program to find factorial of given number
#include <stdio.h>
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}