
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find Factorial
Factorial of a non-negative integer n is the product of all the positive integers that are less than or equal to n.
For example: The factorial of 5 is 120.
5! = 5 * 4 * 3 * 2 *1 5! = 120
The factorial of an integer can be found using a recursive program or a non-recursive program. Example of both of these are given as follows.
Factorial using Non-Recursive Program
A for loop can be used to find the factorial of a number. This is demonstrated using the following program −
Example
#include <iostream> using namespace std; int main() { int n = 5, fact = 1, i; for(i=1; i<=n; i++) fact = fact * i; cout<<"Factorial of "<<n<<" is "<<fact; return 0; }
Output
Factorial of 5 is 120
In the above program, the for loop runs from 1 to n. For each iteration of the loop, fact is multiplied with i. The final value of fact is the product of all numbers from 1 to n. This is demonstrated using the following code snippet.
for(i=1; i<=n; i++) fact = fact * i;
Factorial Using a Recursive Program
The following program demonstrates a recursive program to find the factorial of a number.
Example
#include <iostream> using namespace std; int fact(int n) { if ((n==0)||(n==1)) return 1; else return n*fact(n-1); } int main() { int n = 5; cout<<"Factorial of "<<n<<" is "<<fact(n); return 0; }
Output
Factorial of 5 is 120
In the above program, the function fact() is a recursive function. The main() function calls fact() using the number whose factorial is required. This is demonstrated by the following code snippet.
cout<<"Factorial of "<<n<<" is "<<fact(n);
If the number is 0 or 1, then fact() returns 1. If the number is any other, then fact() recursively calls itself with the value n-1.
This is demonstrated using the following code snippet −
int fact(int n) { if ((n==0)||(n==1)) return 1; else return n*fact(n-1); }
- Related Articles
- C++ Program to Find Factorial of Large Numbers
- Java Program to Find Factorial of a number
- Swift Program to Find Factorial of a number
- Kotlin Program to Find Factorial of a number
- 8085 program to find the factorial of a number
- 8086 program to find the factorial of a number
- C program to find trailing zero in given factorial
- Python program to find factorial of a large number
- C++ Program to Find Factorial of a Number using Iteration
- C++ Program to Find Factorial of a Number using Recursion
- Java Program to Find Factorial of a Number Using Recursion
- Haskell Program To Find The Factorial Of A Positive Number
- C++ Program to Find Factorial of a Number using Dynamic Programming
- C++ program to find first digit in factorial of a number
- Python Program to find the factorial of a number without recursion
