Skip to main content

Creating Multiplication tables using Matlab

Program

Write a program to generate Multiplication tables of a given number using Matlab. Also generate Multiplication table for all numbers at once, like the one below:
Multiplication Table using Matlab
Multiplication Table 20 x 20
To know more about the multiplication tables you can refer:

Solution

Program 1: We will ask the user to give input of a number. Then we will use a for loop to print out the multiplication table of the corresponding number. 
Program 2: We will write a program using functions.
Program 3: We will create 10x10 multiplication table using the solution given here: Stack Overflow. Program 4: Generate 10x10 multiplication table using the nested for loops.
You can refer to the explanation section for better understanding of the program. Some of the concepts you need to know to solve this problem are as follows:
1) input function in Matlab
2) for loop in Matlab - This website has a very good explanation of for loops
3) fprintf 
4) function in Matlab
5) colon operator, Matlab colon operator
6) Transpose of a matrix in matlab, without using the inbuilt transpose
7) nested for loops

Program 1

1
2
3
4
5
number = input('Enter the number you want the multiplication table to be created');

for i = [1:20]
    fprintf('%d x %d = %d\n',number,i,number*i);
end
I have saved this file as multiplication_table.m on my local computer.

Explanation 1

Line 1: We are asking the user to enter the number for which the user wants the multiplication table to be created.
Line 3: We are starting the for loop. Lets understand the for loop by taking an example. Lets say the user has entered the number as 5. Now in the first iteration the value of i is 1. Now the fprintf gets executed. In fprintf we see that there are three parameters - number, the value of i in the current iteration, the value of i with the number. In this first iteration the three values will be 5, 1, 5 respectively. Now the second iteration starts and the values will be 5, 2, 10 respectively. In the similar way all the other values upto 20 gets executed and you will get a nice table.

Program 2


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function generate_table(number)
%this function will generate a multiplication table
%of the input parameter.
%Documentation
%generate_table(x)

for i = [1:20]
    fprintf('%d x %d = %d\n',number,i,number*i);
end
end
I have saved this program as generate_table.m on my local computer.

Explanation 2

This is similar to the above program but here the user will enter the number for which he wants the Multiplication table to be generated as a parameter instead of asking the user using the input function.

Program 3


>>(1:10)' * (1:10)

Explanation 3

I have typed this program in my command prompt and got the desired output. Here we are multiplying two matrices. The first matrix is 1:10 a row matrix which we are using the transpose to create column matrix. And the second one is row matrix. Now we know that if we are multiplying a column vector and a row vector we get a matrix. Here we will get a 10x10 matrix which is like the above one.

Program 4

1
2
3
4
5
6
for i = (1:10)
    for j = (1:10)
        fprintf('%d\t',i*j);
    end
    fprintf('\n');
end
I have saved this file as table_multiplication.m on my local computer.

Explanation 4

Let us understand the above code by looking at each iteration. Now first consider the outer for loop. During the first iteration the value of i becomes equal to 1. As i has got a value the for loop will go inside the loop. Now the second for loop or inside for loop gets executed and j gets a value 1. In the similar way j gets values till 10 and the fprintf in this loop prints(i*j = 1*1(For first iteration of both the loops)) like this:
1    2    3    4    5    6    7    8    9    10
A large gap between the numbers is because of the '\t' which is a escape sequence telling the matlab to give a gap of tab space(4 non braking spaces). As we have reached 10 the condition of the second for loop is false and this loop gets exited giving back the control to the first loop. A fprintf function is invoked to create a line break so that we get the next set of values or the 2 multiplication table printed. In the similar way the 3 multiplication table is also printed. After all the iterations are completed i.e. i = 10 and j = 10 reaches then we can observe the table given in the image.

Try it yourself

1) Write your version of program such that you will ask the user two numbers in which the first number is the number for which the user wants the multiplication table and the second number will be the number of iterations. Comment your program in the comment box below.

Input/Output

Multiplication table generation using matlab
  Multiplication table generation using matlab
Multiplication table generation using Matlab

Final Note

As always thanks for stopping by and if you have any doubt or suggestion or didn't understand anything then please do contact me or comment below in the comment box. I will be really happy to help you. You can contact me from here: contact me
The above program was high lighted using hilite.me.
keywords: multiplication table, transpose, 

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...

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 documenta...