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

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

Problem 11 Project Euler Solution with python

Largest product in a grid In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07

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