C++ Program to find the Factorial of a Number
Factorial of a Number in C++
Here we will discuss how to find the factorial of a number in C++ programming language.
Factorial of any number is the product of it and all the positive numbers below it for example factorial of 5 is 120
Factorial of n (n!) = 1 * 2 * 3 * 4....n
5! = 1 x 2 x 3 x 4 x 5 = 120 7! = 1 x 2 x 3 x 4 x 5 x 6 x 7 = 5040
Ways discussed:-
- Iterative approach
- Recursive approach
Method 1 (Iterative)
C++ Code
Run
#include<iostream>
using namespace std;
int main ()
{
int num = 6, fact = 1;
// Factorial of negative number doesn't exist
// Read more here - https://www.quora.com/Is-the-factorial-of-a-negative-number-possible
if(num < 0)
cout << "Not Possible";
else
{
for(int i = 1; i <= num; i++)
fact = fact * i;
}
cout << "Fact " << num << ": " << fact;
}
// Time complexity: O(N)
// Space complexity: O(1)Output
Fact 6: 720
Method 2 (Recursive)
This method uses recursion in C++
Run
#include<iostream>
using namespace std;
int getFactorial(int num)
{
if(num == 0)
return 1;
return num * getFactorial(num-1);
}
int main ()
{
int num = 8;
int fact = getFactorial(num);
cout << "Fact " << num << " : " << fact;
return 0;
}
// Time complexity: O(N)
// Space complexity: O(1)
// Auxiliary Space complexity (Function call stack): O(N)
Output
Fact 8 : 40320
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python

//using recursion
#include
using namespace std;
int fact(int n)
{
if(n==0)
return 1;
return n*fact(n-1);
}
int main()
{
int n;
cin>>n;
cout<<fact(n);
}