Factorial Program in C++

Factorial Program in C++ | In mathematics, the factorial of a positive integer n, denoted by n! Factorial is the product of all positive integers less than or equal to n. For example:- The factorial of 4= 4! = 4*3*2*1 or 1*2*3*4 = 24

Factorial (n) = 1 * 2 * … * (n-1) * n
Or,
n * (n-1) * … * 2 * 1

The factorial of a negative number doesn’t exist. The value of 0! is 1, according to the convention for an empty product.

Procedure to find the factorial of a number in C++,

1) Take a number
2) Declare a temporary variable fact and initialize it with 1. Since factorial will be a large number so better to take a long data type. long fact = 1;
3) Take an iterator variable i, starting from 1
4) Multiply fact variable and iterator variable. Store the result into the fact variable. fact = fact * i;
5) Increase the iterator variable by 1.
6) Repeat 4 and 5 steps until the iterator variable is less than the input number.

C++ Program to Find Factorial of a Number

Using this we can write a program to find the factorial of a number in C++,

// C++ program to find factorial 
// of a number using For Loop
#include<iostream>
using namespace std;
int main()
{
  // declare variables
  int num;
  long factorial=1;

  // take input
  cout << "Enter a positive integer : ";
  cin >> num;

  // calculate factorial
  for (int i=1; i<=num; i++)
  {
    factorial = factorial * i;
  }

  // display result
  cout << "Factorial = " << factorial << endl;

  return 0;
}

Output for the different test-cases:-

Enter a positive integer: 3
Factorial = 6

Enter a positive integer: 5
Factorial = 120

Enter a positive integer: 10
Factorial = 3628800

Enter a positive integer: 20
Factorial = 2432902008176640000

In this program, we have used a for loop and increment operator, but we can also use decrement operator. Below is the code using for loop and decrement operators,

// calculate factorial
for (int i=num; i > 0; i--)
{
   factorial = factorial * i;
}

We can also use while loop in place of for loop. And in the case of while loop again we can use either increment operator or decrement operator.

// C++ program to find factorial of a number using while Loop
#include<iostream>
using namespace std;
int main()
{
  // declare variables
  int num;
  long factorial=1;

  // take input
  cout << "Enter a positive integer : ";
  cin >> num;

  // calculate factorial using while loop
  while (num != 0)
  {
    factorial = factorial * num;
    num--;
  }

  // display result
  cout << "Factorial = " << factorial << endl;

  return 0;
}

Using Function

Based on the above programs we can develop a function to calculate factorial value. The function will take numbers as arguments and return the factorial value to the called function. The function will be written as,

// function to calculate Factorial value
long findFactorial(int number) 
{
   // declare variable
   long fact = 1;

   // calculate factorial value
   for(int i=number; i>1; i--) {
      fact = fact * i;
   }

   // return result
   return fact;
}

Factorial Program in C++ using Function

Using the above factorial function to calculate the factorial value of a number, we can write a factorial program in C++ using Function. It can be written as below,

// C++ program to find factorial 
// of a number using while Loop
#include<iostream>
using namespace std;
long findFactorial(int);
int main()
{
  // declare variables
  int num;

  // take input
  cout << "Enter a positive integer : ";
  cin >> num;

  // display result
  cout << "Factorial = " << findFactorial(num) << endl;

  return 0;
}

// function to calculate Factorial value
long findFactorial(int number) 
{
	// declare variable
	long fact = 1;
		// calculate factorial value
	for(int i=number; i>1; i--) {
		fact = fact * i;
	}
		// return result
	return fact;
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *