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

Problem 60 Project Euler Solution with python

Prime pair sets The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. This problem is j u st a brute force problem. If you have come here because you don't know the limit upto which you will h ave to gener ate the prime numbers t hen go ahe ad and t r y with 10,000 . When I first start ed solving the problem I chose 1 million(beca use most of the problem s on project E uler have this limit ), but it took very long for the computer to fin d the solution. After searching on the internet then I found many people choosing 10, 000 so I have changed my in put f rom 1 million to 10000 and the output was f ast. He

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 tag Us

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documentation