Open In App

Factorial in MATLAB

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. In this article, we’ll be calculating the factorial of a number n using MATLAB’s built-in function ‘factorial(number)’.

Factorial:

The factorial of a non-negative integer is the multiplication of all positive integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720. The factorial of a number n is represented as n!

0! = 1

1! = 1

Now suppose we want to find a Factorial of 6. 

Example 1:

Matlab




% MATLAB code for find factorial 
ans = factorial(6);
disp(ans);


Output:

output

Now take another example to find a factorial of 22.

Example 2:

Matlab




% MATLAB code for find factorial
format long;
ans = factorial(22);
disp(ans);


Output:

 

In the given below example, we find the factorial of matrix elements.

Example 3:

Matlab




% MATLAB code to find the factorial of matrix elements.
% Input Matrix
input = [[1,5,8];[4,6,0]];
disp(input);
  
% Output Matrix
output = factorial(input);
disp(output);


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads