Skip to main content

Factorial of a number using Matlab

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:
  1. Matlab Functions
  2.  Vectors in Matlab
  3. For loop in Matlab
  4. If else in Matlab

   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
To download this program in matlab file format then click here: facto.m

Explanation

Here we will use a series of if, elseif, and else statements to find the factorial of the number.
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 = 1

Second 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

The above code was high lighted using hilite.me.
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

Popular posts from this blog

Project Euler Problem 67 Solution with Python

Maximum path sum II By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.

Problem 43 Project Euler Solution with python

Sub-string divisibility The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. Let d 1 be the 1 st digit, d 2 be the 2 nd digit, and so on. In this way, we note the following: d 2 d 3 d 4 =406 is divisible by 2 d 3 d 4 d 5 =063 is divisible by 3 d 4 d 5 d 6 =635 is divisible by 5 d 5 d 6 d 7 =357 is divisible by 7 d 6 d 7 d 8 =572 is divisible by 11 d 7 d 8 d 9 =728 is divisible by 13 d 8 d 9 d 10 =289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. One might write a simple solution using direct if else statements for this problem. Using if else statements, the execution time may be a few seconds. But this is not a very good approach. I too had written a program with if else statement which will take each and every permutation of the 0-9 Pandigital and check for the conditions given in the qu...

Add/Embed SVG to Blogger website

In this post I will tell you my method(trick) of adding SVG images in a blogger website or blog. Before starting , the first thin g I am assu m ing is that you are aware of SVG if you are here. If not please see S calable V ec tor G raphics Recently when I tried to embed a SVG image for a post on pygal, I tried uploading the SVG file and blogger Image uploader came up with an error, because of which I had to find some other way.  SVG File upload Error in Blogger  I started sea rc hing Google " Embed SVG in Blogger " . I found blogorrhea , w h ich gave some i nformatio n on add ing SVG directly as a markup , which worked , but I faced another problem using this . Also th is guy has used lot of Javascript which was confusin g for me, being new to using SVG.   So I first t houg ht of learning on h ow to embed SVG in HTML and t his on e worked out. Actually we can embed SVG in HTML i n following ways: Using Object tag Using Iframe tag Using embed...