
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Factorial
A factorial of a non-negative integer 'n,' denoted as 'n!', is defined as the product of all positive integers from 1 to 'n.' So mathematically the syntax is −
n! = 1 × 2 × 3 × ... × n
So for example factorial of 5 is going to be
5! = 1 × 2 × 3 × 4 × 5 = 120
Syntax
result = factorial(n)
- Here n is the non-negative integer for which you want to calculate the factorial.
- Result will store the calculated factorial value.
In mathematical notation, n factorial, denoted as n!, is frequently represented with an exclamation point. It's important to note that in MATLAB, using n! as a syntax for computing the factorial of n is not valid.
Examples on Calculating Factorials
Let us see a few examples on calculating factorials of a given number.
Example 1: Calculate factorial of number n
To calculate 5!, you can use the factorial() function in MATLAB −
result = factorial(5)
When you execute in matlab command window the output is −
>> result = factorial(5) result = 120
The above code will give you the result: result = 120, because 5! is equal to 120.
Example 2: Factorial of a Large Number
MATLAB can handle factorials of even large numbers.
n = 20; result = factorial(n)
When you execute the same in matlab command window the output is −
>> n = 20; result = factorial(n) result = 2.4329e+18
Example 3: Calculating Factorials of an Array
Suppose you have an array A with several values for which you want to calculate the factorials −
A = [3, 4, 5, 6, 7]; result = factorial(A)
The output is −
result = 6 24 120 720 5040
Example 4: Calculating Factorials of an Array
Consider the array shown below , for which we are going to calculate the factorial.
num_array = [0 1 2; 3 4 5]; result = factorial(num_array)
On execution the output is −
result = 1 1 2 6 24 120
Example 5: Factorial of Unsigned Integers
uints = uint64([7 10 15 20]); result = factorial(uints)
So we have vector of unsigned ints as [7 10 15 20]
On execution the output is −
result = 5.0400e+03 3.6288e+06 1.3077e+12 2.4329e+18
Factorial Calculation Using Functions in Matlab
You can create a MATLAB function to calculate the factorial of a number. Here's a simple function to do that −
function fact = factorial_custom(n) if n < 0 error('Factorial is undefined for negative numbers.'); elseif n == 0 || n == 1 fact = 1; else fact = 1; for i = 2:n fact = fact * i; end end end
This factorial_custom function takes an input n, and it calculates the factorial of n using a for loop. It handles negative numbers and returns an error message for them. For 0 and 1, the factorial is defined as 1. For other positive integers, it calculates the factorial using a loop.
You can execute in matlab as follows −

Calculate Factorial Using For Loop
You can calculate a factorial using a for loop as follows −
n=6; result = 1; for i = 1:n result = result * i; end
Here, result is initialized to 1, and the loop multiplies it by the numbers from 1 to n.
When you execute the same in matlab command window −
>> n = 6; result = 1; for i = 1:n result = result * i; end >> result result = 720
Calculate Factorial Using While Loop
A factorial can also be calculated with a while loop −
n = 6; result = 1; i = 1; while i <= n result = result * i; i = i + 1; end
This code is similar to the for loop method but uses a while loop instead.
The execution in matlab command window is as follows −
>> n = 6; result = 1; i = 1; while i <= n result = result * i; i = i + 1; end >> result result = 720