# C++ Program to Find Factorial of a Number

## Grammarly

- More than 40 Lakhs Students use Grammarly for their spell and grammar checks. You can register and try for free.

In this post you will learn how to Find Factorial of a Number.

This lesson will teach you how to find the factorial of a number, with a for loop and a mathematical function using the C++ Language. Let’s look at the below source code.

## How to Find Factorial of a Number?

## Source Code

#include <iostream> using namespace std; int main() { int i,fact=1,num; cin>>num; cout<<"Enter any Number: " <<num<<endl; for(i=1;i<=num;i++) { fact=fact*i; } cout<<"\nFactorial of " <<num<<" is: "<<fact<<endl; return 0; }

## Input

```
5
```

## Output

Enter any Number: 5 Factorial of 5 is: 120

**#include <iostream>**

- This line which is called the
*header file.*`#include`

statement tells the compiler to use available files and`<iostream>`

is the name of the specific that we have used in this code. The`<iostream>`

file stands for**Input**and**Output**statement.

**using namespace std;**

- The C++ has a
**standard library**that has files for different functions and this line is used to access the standard file for input and output statements.

**int main();**

- This line usually controls the
**function**of the code, as it calls the functions to perform their tasks. - The
`int main()`

shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement**‘return 0;’.**

**{ }**

- The opening
**‘ { ‘**and the closing**‘ } ‘**curly braces mark the start and the finish of the main function - Every statement and value between these braces belong to the main function.

The above statements are the main factors that **support** the function of the source code. Now we can look into the working and layout of the code’s function.

- A
**Factorial**is the product of an integer and all the integers below it. For example**4! = 1*2*3*4 = 24.**The symbol**!**represents the factorial function. - Using this we have created a source code to find the factorial of a number.
- First we declare the variables
*i, num, fact*as**integers**and we initialize the integer**fact = 1.**Then collect the number from the user and store it in**num**using function`cin>>`

,display the value using`cout<<`

and the**Insertion Operators'<<‘ , ‘>>’.** - We then use the
**for loop.**A for loop is used in C++ language to repeat a function continuously until the condition is satisfied. The formula to find the sum of the natural numbers is written in the form of a for loop. The syntax of the for loop isfor (variable initialization; condition; increment operation) { //loop statements; }

- We can work on the for loop in the above source code to understand better
for(i=1;i<=num;i++) { fact=fact*i; }

- In the for loop first is the
**initialization of the variable**`i = 1`

next the**condition to be satisfied**`i <= num`

which is followed by**increment operator**`i++`

. - The next line in the
*for loop*is thewhich is executed after the**loop statement***for loop*is run. - The loop statement is
`fact=fact*i`

where the initial value of**fact**is**1**and**i**is**1**. Now when the*for loop*is executed, if the condition is satisfied, i.e**i**is less than or equal to**num**, the value**i**is in incremented by one and the mathematical loop statement is executed. - Now the value of
**fact**changes as per the loop statement function. Initially the value ofis*fact***1**so when it is multiplied by**2**the incremented value of**i**, the value of**fact**becomes**2**. Then the function returns to the*for loop*where the value of**i**is*incremented*and the loop statement is executed again where the value of**fact = 2**is multiplied with the incremented value of**i = 3.** - The
*for loop*is executed repeatedly until the value**i**is**not**less than or equal to**num.**Now the function terminates and the last*fact*value is the Factorial of the Number.

**Note:** The **‘ << endl ‘ **in the code is used to **end the current line **and move to the next line and **‘\n’ **is also a **new line function,** to understand how both the functions work exclude it from the code, move it around and work with it.

## Additional Source Code

You can also try the source code given below. The factorial of a negative number does not exist. Using this fact the following code is structured using decision making statements, for loop and a mathematical function. Let’s look at the below source code.

#include <iostream> using namespace std; int main() { int n,factorial = 1; cin >> n; cout << "Enter a positive integer: "<< n << endl; if (n < 0) cout << "\nError! Factorial of a negative number doesn't exist."; else { for(int i = 1; i <= n; ++i) { factorial *= i; } cout << "\nFactorial of " << n << " = " << factorial; } return 0; }

## Input

`-5`

## Output

Enter a positive integer: -5 Error! Factorial of a negative number doesn't exist.

- In this source code after the declaring the required variables as
**integers**and collecting the value from the user, we have included a function to check if the number is**positive**or**negative**`(n < 0)`

using the**decision making statements**`if else`

- If the number is negative the
**if**statement output statement is displayed, if it is positive the**else**statement is executed which includes the**for loop**like the previous source code with the similar condition and mathematical function. - As the number is positive the factorial of the number is calculated and the output is displayed.
- The above source code is another way to find the
**Factorial of a Number.**