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

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