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 20 x 20 |
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
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
|
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
|
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 |
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,