Introduction

Control Statement

- Calculator Program
- Program to identify day of week
- Program to print Fibonacci series
- Check Whether the Number is Even or Odd in C++
- Program to check whether the number is prime or not
- Program to check vowel or constant
- Program to check the largest number among three numbers
- Program to the sum of natural numbers
- Program to check leap year

Function

- Program to find LCM
- Program to check prime number using Functions
- Program to convert Binary to Decimal Number
- Program to convert Decimal to Binary Number
- Program to convert Binary to Octal Number
- Program to convert Octal to Binary Number
- Program to find reverse of the sentence using Recursion
- Program to Shutdown and restart Computer
- Program to find the area of triangle

Array

Pointer

OOPS

No Examples found for this topic - CodeHelpPro

# Program to find factorial of the number

In this example, we will learn to find the factorial of any number using C++ programming language.\

To understand this example, You must knowledge in following topics.

As we know, Factorial is the multiplication of numbers from 1 to that number. The value of factorial of any number must be greater than 1.

Lets look and example to make clear concepts

`6! = 6 X 5 X 4 X 3 X 2 X 1 = 720`

In this example, We will do the same action. We will run a for-loop from 1 to that number and multiply each number and store it in one variable. Later we will print that variable.

```
#include <iostream>
using namespace std;
int main()
{
int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
cout << "The factorial of negative number doesn't exists.";
else {
for(int i = 1; i <=n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}
```

The output of above program is

```
Enter a positive integer: 6
Factorial of 6 = 720
Enter a positive integer: -2
The factorial of negative number doesn't exists.
```

Subscribe

Login

0 Comments

Inline Feedbacks

View all comments