## Q. Write a program to find factorial of a given number.

Factorial :- The factorial of a number is the product of all the integers from 1 to that number.

For Example :- Factorial of 5 = 120 (5*4*3*2*1).

## Factorial Algorithm

```START
step 1 : Read the number n
step 2 : [Initialize]
i=1, fact=1
step 3 : Repeat step 4 through 6 until i=n
step 4 : fact=fact*i
step 5 : i=i+1
step 6 : Print fact
STOP
```

# Factorial Program

• C
• C++
• Java
• Python
• C#
• PHP
• ```#include <stdio.h>
int main() {
int num=7, i;
unsigned long long fact = 1;
if (num < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= num; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", num, fact);
}
return 0;
}
```
```#include <iostream>
using namespace std;
int main()
{
unsigned int num=7;
unsigned long long factorial = 1;
for(int i = 1; i <=num; ++i)
{
factorial *= i;
}
cout << "Factorial of " << num << " = " << factorial;
return 0;
}
```
```public class LFC {
public static void main(String[] args) {
int num = 7;
long factorial = 1;
for(int i = 1; i <= num; ++i)
{
// factorial = factorial * i;
factorial *= i;
}
System.out.printf("Factorial of %d = %d", num, factorial);
}
}
```
```num = 7
# To take input from the user
#num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Error, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("Factorial of",num,"=",factorial)
```
```using System;
public class LFC
{
public static void Main(string[] args)
{
int i,fact=1,num=7;
for(i=1;i<=num;i++){
fact=fact*i;
}
Console.Write("Factorial of " +num+" = "+fact);
}
}
```
```\$num = 7;
\$fact = 1;
for (\$x=\$num; \$x>=1; \$x--)
{
\$fact = \$fact * \$x;
}
echo "Factorial of \$num = \$fact";
```
```#include <stdio.h>
int main() {
int num=7, i;
unsigned long long fact = 1;
if (num < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= num; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", num, fact);
}
return 0;
}
```

#### Output

```Factorial of 7 = 5040
```

## Recommended Programs

Program to check whether number is even or odd
Program to count number of digits in a number