Courses

# Program to find factorial of the number

0 Suresh Chand March 28, 2021

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
Notify of
Inline Feedbacks

Get the latest tutorials, examples and projects updates.

!