Write a program to find the factorial of a given number using
Matlab. Use the function name as facto(x), where x is the number that the user gives
the input to find the factorial of ‘x’.
Solution
The above question was asked to find the factorial of a given
number x. So what is a factorial of a number? That means we have to find
x!
= x(x-1)(x-2)……….(3)(2)(1)
Where, x! is the mathematical representation of x-factorial.
Let us consider the number, user gives input as 5 then we
have to find
5!
= 5(5-1)(5-2)(5-3)(5-4)
5!
= 5(4)(3)(2)(1)
5!
= 120
Note: It
should be remembered that the value of 0! (0-factorial) is 1 and the range of x
is in I+( Positive Integers ).
The above was a brief introduction about factorial. To know
more about factorial then please visit:
The solution to the program is very simple. I have already
added few comments to the program so that the program will be easy to
understand. But if you couldn’t understand the program then please see the
explanation section below the program section so that you can have a look at
the explanation of the program. But if you couldn’t understand the program and
also the explanation, then have a look at the links below so that you will be
understanding the basics behind the Matlab program used here:
Program
function result = facto(x) %this function will generate factorial of a given number %the output of the given input will be as follows: % x(x-1)(x-2)(x-3).....(3)(2)(1) % also if x = 0 then the value is 1 and also x is +integer %If statement to check if the number is negative if x<0 result = 'Negative numbers are not valid for factorial' % else if the number is not an integer then we will convert the number to % integer and find the value of the factorial elseif int64(x) ~= x fprintf('You have entered a number which is not an integer\n'); fprintf('Please enter a valid integer to find the factorial\n'); elseif x == 0 result = 1; else result = 1; for i = 1:x result = result*i; end end
First we will check if the number is less than 0 and if it is less than 0 then we will give the result as Negative numbers are not valid for factorial. If the value is not an integer then we will prompt the user that the value entered is not and integer and ask the user to enter a valid integer to find the factorial. Next if the number is equal to zero then the result is directly given as 1. Finally if the number is a positive integer then:
we will initiate the result as 1, from here we will go loop by loop to understand this program easily. Here let us consider the input as 3 for the example sake.
First iteration
The value of i is 1 and this is multiplied to the result which will give the value as 1*1 = 1Second iteration
The value of i is 2 and this is multiplied to the result which will give the value as 1*2 = 2.Third iteration (Final iteration)
The value of i is 3 and this is multiplied to the result which will give the value as 2*3 = 6.So finally when we combine all the iterations, the calculations that take place iteration by iteration are as follows:
1*2*3 = 3*2*1 = 3*(3-1)*(3*2) = 6
Remember that Matlab has already created a function to find the factorial of the number easily without writing any programs.It is factorial(n), and you can find the documentation of the factorial function in matlab from here: Factorial of input - Matlab
I have tried to explain the code and all the other things in such a way that they are easy to understandable even for the beginners. If you haven't understood any part of this post please let me know about your doubt or the question, by contacting me here: Contact me
Run your code in the matlab software and enter your output in the comment below.
Keywords: factorial, matlab functions, facto function