In this blog post, we will write a program in C, C++, Python, and Java to calculate the factorial of a user-entered integer.

To write a program, the following are the prerequisites:

- Data types
- Programming operators
- if...else statement
- for loop

##
**
What is a Factorial?
**

Factorial is a mathematical concept that uses the ! symbol and is used in permutation, combination, and probability. A factorial of a number, n, is the product of that number with every whole number less than or equal to n.

For example, a factorial of 3 (3!) = 3*2*1 = 6.

Here is another example, 5! = 5*4*3*2*1 = 120.

*
Note
- The factorial of 0 (0!) is equal to 1, and the factorial of a negative number does not exist.
*

###
**
Uses or Applications of a Factorial
**

- Recursion
- Permutation
- Combination
- Probability distribution

##
**
C Program to Find the Factorial of an Integer
**

```
#include<stdio.h>
#include<conio.h>
void main()
{
long num, i,fact=1;
clrscr();
printf("Enter a number:");
scanf("%ld",&num);
for(i=num; i>=1; i--)
{ fact*=i;
}
printf("The factorial of the number %ld is %ld",num,fact);
getch();
}
```

**
Output:
**

```
Enter a number:10
The factorial of the number 10 is 3628800
```

##
**
C++ Program to Find the Factorial of an Integer
**

```
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
int num,fact=1;
cout<<"Enter a number: ";
cin>>num;
for(int i=1; i<=num; i++)
fact *= i;
if(num==0)
cout<<"The factorial is 1";
else
cout<<"The factorial of "<<num<<" is "<<fact;
getch();
}
```

**
Output:
**

```
Enter a Number: 5
The factorial of 5 is 120
```

##
**
Python Program to Find the Factorial of an Integer
**

```
num =int(input("Enter a Number: "))
fact=1
for i in range(1,num+1):
fact*=i
if num==0:
print("The factorial is 1")
else:
print("The factorial of",num,"is",fact)
```

**
Output:
**

```
Enter a Number: 6
The factorial of 6 is 720
```

##
**
Java Program to Find the Factorial of an Integer
**

```
import java.util.*;
public class Main
{
public static void main(String[] args) {
int i, fact=1;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = s.nextInt();
if (num == 0)
{
System.out.println("The factorial is 1");
}
else
{
for(i=1; i<=num; ++i)
{
fact=fact*i;
}
System.out.println("The factorial of " +num+ " is " +fact);
}
}
}
```

**
Output:
**

```
Enter a number: 5
The factorial of 5 is 120
```

##
**
Conclusion
**

Wasn't it easy to write a program to calculate the factorial of any number? This is a very basic program. You can add a condition for negative numbers, i.e., if a user enters a negative number, it must display the message "The factorial of a negative number does not exist".

*
We hope this article has helped you write a program to calculate the factorial of an integer. If you have any doubts, do let us know in the comments.
*

**
People are also reading:
**

- WAP to swap two numbers
- WAP in C++ and python to convert temperature from Fahrenheit to Celsius and vice-versa
- WAP in C++ and python to check whether the year is a leap year or not
- Perfect Number in C
- WAP in C++ & Python to reverse a number
- WAP in C++ & Python to calculate the size of each data types
- WAP to print the truth table for XY+Z
- C++ and Python Code to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix
- Linear Search in Python and C++

## Leave a Comment on this Post