# Write a program to find multiplication of 2 matrix Since, the program is long and hard to debug, it is better to solve this program by passing it to a function. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a 3 Enter elements a -2 Enter elements a 5 Enter elements a 3 Enter elements a 0 Enter elements a 4 Enter elements of matrix 2: Enter elements b 2 Enter elements b 3 Enter elements b -9 Enter elements b 0 Enter elements b 0 Enter elements b 4 Output Matrix: 24 29 6 25 It is better to solve this program by passing it to a function.

The column of first matrix should be equal to row of second matrix for multiplication. To understand this example, you should have the knowledge of following C programming topics: C Multidimensional Arrays To multiply two matrices, the number of columns of first matrix should be equal to the number of rows to second matrix.

Then, user is asked to enter two matrix and finally the output of two matrix is calculated and displayed. This program displays the error until the number of columns of first matrix is equal to the number of rows of second matrix. Check out these related examples:. If this condition is not satisfied then, the size of matrix is again asked using while loop. Here We've used three for loop to multiply the matrices.

### Write a program to multiply two matrices in java

Beginner Friendly Tutorials for Programmers What do you want to learn today? Same Program in Other Programming Language You may also like same program in other programming languages:. Here We've used three for loop to multiply the matrices. The column of first matrix should be equal to row of second matrix for multiplication. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a 3 Enter elements a -2 Enter elements a 5 Enter elements a 3 Enter elements a 0 Enter elements a 4 Enter elements of matrix 2: Enter elements b 2 Enter elements b 3 Enter elements b -9 Enter elements b 0 Enter elements b 0 Enter elements b 4 Output Matrix: 24 29 6 25 In this program, user is asked to enter the size of two matrix at first. Beginner Friendly Tutorials for Programmers What do you want to learn today? This program displays the error until the number of columns of first matrix is equal to the number of rows of second matrix. The first two for loop is used for row and column, whereas the third one is used to apply the multiplication rule of matrix Apply the matrix multiplication rule and multiply the matrix, after getting multiplication result, store the value inside the sum variable, after multiplying each and every row elements of first matrix with corresponding each and every column elements of second matrix, and initialize the value of sum variable into the third matrix one by one. Here is the sample run, below is the first screenshot: The second screenshot after providing row size, column size, and matrix element of first matrix: The third screenshot after providing row size, column size, and matrix element of second matrix.

Same Program in Other Programming Language You may also like same program in other programming languages:. Beginner Friendly Tutorials for Programmers What do you want to learn today? Here we have provided 3 as row size of second matrix which is equal to the column size of first matrix: Now let's take another sample run.

### Matrix multiplication in c using function

Since, the program is long and hard to debug, it is better to solve this program by passing it to a function. Then, user is asked to enter two matrix and finally the output of two matrix is calculated and displayed. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a 3 Enter elements a -2 Enter elements a 5 Enter elements a 3 Enter elements a 0 Enter elements a 4 Enter elements of matrix 2: Enter elements b 2 Enter elements b 3 Enter elements b -9 Enter elements b 0 Enter elements b 0 Enter elements b 4 Output Matrix: 24 29 6 25 In this program, user is asked to enter the size of two matrix at first. Same Program in Other Programming Language You may also like same program in other programming languages:. Here in this case, let's suppose user has entered the column size of first matrix as 3 and rwo size of second matrix as 2 which is not equal: As you can clearly see that from the above sample run, the matrix multiplication is not possible, if the column size of first matrix is not equal to the row size of the second matrix. Never forgot to initialize 0 to sum before multiplying process starts for each index of third matrix In this way the third matrix say mat3 contains total of 9 elements that will be the multiplication result of the two given matrix say mat1 and mat2 Finally print the value of third matrix Now let's modify the above program by implenting extra feature. Beginner Friendly Tutorials for Programmers What do you want to learn today? The column of first matrix should be equal to row of second matrix for multiplication. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a 3 Enter elements a -2 Enter elements a 5 Enter elements a 3 Enter elements a 0 Enter elements a 4 Enter elements of matrix 2: Enter elements b 2 Enter elements b 3 Enter elements b -9 Enter elements b 0 Enter elements b 0 Enter elements b 4 Output Matrix: 24 29 6 25 It is better to solve this program by passing it to a function. Then, the program multiplies these two matrices if possible and displays it on the screen. This program displays the error until the number of columns of first matrix is equal to the number of rows of second matrix.

Here in this case, let's suppose user has entered the column size of first matrix as 3 and rwo size of second matrix as 2 which is not equal: As you can clearly see that from the above sample run, the matrix multiplication is not possible, if the column size of first matrix is not equal to the row size of the second matrix.

Then, the program multiplies these two matrices if possible and displays it on the screen.

Rated 6/10 based on 91 review
Download
C Program to Multiply Two Matrices