C++ to find the factorial of a number using class

C++ to find the factorial of a number using a class in 2 ways:

In this post, we will learn how to find the factorial of a number with a class in C++. The factorial of a number is equal to the multiplication of all numbers from 1 to that number.

Using a class, we can put all the factorial-related operations in the class and one method can be exposed to calculate the factorial. We can create one object of the class and call the factorial method of that class to find the factorial.

It will make the code clean if we move all related functions into one place. With this C++ program, you will learn how to create a different class to calculate the factorial of a number.

Method 1: C++ program to calculate factorial with a class:

The following program uses a separate class Factorial to calculate the factorial of a user-given number:

#include <iostream>
using namespace std;

class Factorial
{
private:
    int num;
    unsigned long long factorial = 1;

public:
    void calculateFactorial();
    void show();
};

void Factorial::calculateFactorial()
{
    cout << "Enter a number:" << endl;
    cin >> num;

    if (num == 0 || num == 1)
    {
        factorial = 1;
    }
    else
    {
        while (num > 1)
        {
            factorial = factorial * num;
            num--;
        }
    }
}

void Factorial::show()
{
    cout << "Factorial: " << factorial << endl;
}

int main()
{
    Factorial factorial;
    factorial.calculateFactorial();
    factorial.show();
}

Download the program on Github

Explanation:

Here,

  • The Factorial class is used to calculate the factorial with user-input value.
  • The num is an integer variable to assign the number. The variable factorial is an unsigned long long variable to hold the factorial value. The maximum value of unsigned long long is 2^64-1 or 18446744073709551615. The program will fail if the final factorial result is more than 18446744073709551615.
  • The calculateFactorial and show are two public functions. The calculateFactorial function is used to calculate the factorial. The calculateFactorial function reads the user input number and calculates the factorial. It uses one while loop to calculate the factorial. It multiplies all numbers from num to 1.
  • The show function is used to show the factorial value, i.e. the value of the factorial variable.

Output:

Enter a number: 
4
Factorial: 24

Enter a number: 
5
Factorial: 120

Method 2: By using a recursive static method:

In the above example, we have used two non-static methods to read the user input and calculate the factorial. We can also use a static method to calculate the factorial. The main method will get the number from the user and use this method to get the factorial. The advantage of a static method is that we can access this method without creating an instance of the class.

#include <iostream>
using namespace std;

class Factorial
{
public:
    static unsigned long long calculateFactorial(unsigned long long num)
    {
        if (num == 0 || num == 1)
        {
            return 1;
        }
        return num * calculateFactorial(num - 1);
    }
};

int main()
{
    int num;

    cout << "Enter a number :" << endl;
    cin >> num;

    unsigned long long factorial = Factorial::calculateFactorial(num);
    cout << "Factorial: " << factorial << endl;
}

Download the program on Github

  • The calculateFactorial method is a recursive method. It returns 1 if the parameter num is either 0 or 1. Else it multiplies the value of num with the factorial value of num - 1. It uses recursive calls to find a factorial.
  • Since the method is a static method, we can access it without creating an instance of the Factorial class.

C++ find factorial with class

It will print similar output:

Enter a number :
20
Factorial: 2432902008176640000

You might also like: