In this tutorial, you will learn how to write C++ programs to find factorial of a given number using loop statements, or recursion technique.

C++ Factorial Program

In C++, you can find the factorial of a given number using looping statements or recursion techniques.

Following picture has the formula to calculate the factorial of a number. And also factorial examples for numbers 5 and 7.

C++ Factorial Programs

1. Factorial using While loop

In this example, we shall make use of C++ While Loop, to find the factorial of a given number.

Algorithm

We shall implement the following factorial algorithm with while loop.

  1. Start.
  2. Read number to a variable n. [We have to find factorial for this number.]
  3. Initialize variable factorial with 1.
  4. Initialize loop control variable i with 1.
  5. Check if i is less than or equal to n. If the condition is false, go to step 8.
  6. Multiply factorial with i.
  7. Increment i. Go to step 5.
  8. Print factorial.
  9. Stop.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int n = 5;
   int factorial = 1;

   int i = 1;
   while (i <= n) {
      factorial *= i;
      i++;
   }

   cout << factorial;
}

Run the above C++ program, and you shall get the following output.

120

2. Factorial using For loop

In this example, we shall use C++ For Loop to find the factorial of a given number. The algorithm would be same as that of the one used in above example using while loop.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int n = 5;
   int factorial = 1;

   for (int i = 1; i <= n; i++)
      factorial *= i;

   cout << factorial;
}

Run the above program, and you shall get the following output for n=5.

5! = 120

3. Factorial using Recursion

Finding Factorial of a number is a classic example for recursion technique in any programming language.

In this example, we shall write a recursion function that helps us to find the factorial of a number.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int factorial(int n);

int main() {
   cout << factorial(4) << endl;
   cout << factorial(6) << endl;
   cout << factorial(5) << endl;
   cout << factorial(0) << endl;
}

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

Following is the output to the above C++ program.

Output

24
720
120
1

4. Factorial using Ternary Operator

When using recursion technique, instead of if else as in above example, you can also use C++ Ternary Operator.

In this example, we shall use recursion technique with ternary operator to make the code concise.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int factorial(int n);

int main() {
   cout << factorial(4) << endl;
}

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

Run the program to find factorial of 5. You can use the factorial() , from the above program, function in your program and call it, to find the factorial of any given n.

Output

24

Conclusion

In this C++ Tutorial, we learned how to write C++ programs to find the factorial of a given number using loop statements and recursion technique.