Tuesday 19 November 2013

MATLAB CODE : Factorial of a Number

Read the following books to get the basic knowledge in MATLAB


FACTORIAL OF A NUMBER :
The below code computes the factorial of a number in MATLAB :


function y = fact(n)
% The function to calculate the factorial of a number
y = n

if n == 0
    y = 1

else
    % All the integers are multiplied one at a time
    y = y * fact(n-1)
end 

The input number should be given as an argument to the function fact()

Output :

0! = 1
1! = 1
2! = 2
3! = 6

No comments:

Post a Comment