Factorial Program in C++

In this article, we will discuss what is a factorial number, and what is its logic to implement it in C++. We will look and see multiple approaches to implement factorial programs.
Firstly, let’s see what a Factorial Number is?

Factorial Number

Calculation of factorial numbers is simple mathematics. The factorial of a number is 1 2 3 number.
For Example, factorial 6 is
6! = 1 2 3 4 5 * 6 = 720.

Explanation of Program

Let’s create a factorial program using recursive functions. Until the value is not equal to zero, the recursive function will call itself.
Factorial can be calculated using the following recursive formula:
n! = n * (n – 1)!
n! = 1 if n = 0 or n = 1

Approach 1: (Using Recursion)

  1. Build a recursive function factorial which takes an integer n as a parameter and returns an unsigned integer as the output.
  2. If n ==0 or n==1, then return 1.
  3. Else return n * factorial(n – 1).

Code Implementation (Using Recursion)

#include <iostream>
using namespace std;

unsigned int factorial(unsigned int n)
{
	if (n == 0 || n == 1)
		return 1;
	return n * factorial(n - 1);
}

int main()
{
	int num = 5;
	cout << "Factorial of "
		<< num << " is: " << factorial(num) << endl;
	return 0;
}

Time complexity: O(n) will be the time complexity for implementing a factorial program in C++ using recursion.

Auxiliary Space: O(n) will be the space complexity for implementing a factorial program in C++ using recursion.

Approach 2: (Using For loop)

Follow the steps to solve the problem:

  1. Using a for loop, we will write a program for finding the factorial of a number.
  2. An integer variable with a value of 1 will be used in the program.
  3. With each iteration, the value will increase by 1 until it equals the value entered by the user.
  4. The factorial of the number entered by the user will be the final value in the fact variable.

Code Implementation (Using For Loop)

#include <iostream>
using namespace std;

unsigned int factorial(unsigned int n)
{
	int res = 1, i;
	for (i = 2; i <= n; i++)
		res *= i;
	return res;
}

int main()
{
	int num = 5;
	cout << "Factorial of "
		<< num << " is "
		<< factorial(num) << endl;
	return 0;
}

Time complexity: O(n) will be the time complexity for implementing a factorial program in C++ using a for loop.

Auxiliary Space: O(1) will be the space complexity for implementing a factorial program in C++ using a for loop.

Approach 3: In this approach, we will see how we can use the while loop for implementing the factorial of a number.

Code Implementation (Using While Loop)

#include <iostream>
using namespace std;
unsigned int factorial(unsigned int n)
{
	if(n == 0)
		return 1;
	int i = n, fact = 1;
	while (n / i != n) {
		fact = fact * i;
		i--;
	}
	return fact;
}
int main()
{
	int num = 5;
	cout << "Factorial of "
		<< num << " is "
		<< factorial(num) << endl;
	return 0;
}

Time complexity: O(n) will be the time complexity for implementing a factorial program in C++ using a while loop.

Auxiliary Space: O(1) will be the space complexity for implementing a factorial program in C++ using a while loop.

Approach 4: A ternary operator can be thought of as a shorthand for an if…else statement. The conditions are provided, along with statements to be executed based on them. Here’s the program for factorial using a ternary operator.

Code Implementation (Using Ternary Operators)

#include <iostream>
using namespace std;

int factorial(int n)
{
	return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}

int main()
{
	int num = 5;
	cout << "Factorial of " << num << " is "<< factorial(num);
	return 0;
}

Time complexity: O(n) will be the time complexity for implementing a factorial program in C++ using a ternary operator for the recursive approach.

Auxiliary Space: O(n) will be the space complexity for implementing a factorial program in C++ using a ternary operator for the recursive approach.

Tips
Various service based companies like TCS, Wipro, Accenture and Capgemini have asked for a factorial program for their technical interviews.

Conclusion
This article tried its best to explain the various approaches to implement factorial programs in C++. Having knowledge about mathematical topics like factorial numbers will always help in the logic building. And for beginners, this problem must be solved to improve their coding as well as problem solving skills.

Other C++ Programs

C++ Program to Add Two Numbers
C++ Program to Reverse a Number
C++ program for heap sort
Bubble Sort in C++ with Program Code and Example
Hello world program in C++
Prime Number Program in C++

Leave a Reply

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