1double[][] c = new double[N][N];
2for (int i = 0; i < N; i++)
3{
4 for (int j = 0; j < N; j++)
5 {
6 for (int k = 0; k < N; k++)
7 {
8 c[i][j] += a[i][k] * b[k][j];
9 }
10 }
11}
12
1#include <stdio.h>
2
3void main ()
4{
5 int i, j, k, m1, n1, m2, n2;
6 int matrix1[10][10],matrix2[10][10],mult[10][10];
7
8 printf("Enter number of rows of matrix 1 : ");
9 scanf("%d", &m1);
10 printf("Enter number of columns of matrix 1 : ");
11 scanf("%d", &n1);
12
13 printf("\n");
14
15 for (i = 0; i < m1; i++)
16 {
17 for (j = 0; j < n1; j++)
18 {
19 printf("Enter element of matrix 1[%d][%d]: ", i, j);
20 scanf("%d", &matrix1[i][j]);
21 }
22 }
23
24 printf("\n");
25
26 printf("Enter number of rows of matrix 2 : ");
27 scanf("%d", &m2);
28 printf("Enter number of columns of matrix 2 : ");
29 scanf("%d", &n2);
30
31 printf("\n");
32
33 if(m1==n2)
34 {
35 for (i = 0; i < m2; i++)
36 {
37 for (j = 0; j < n2; j++)
38 {
39 printf("Enter elements of matrix 2[%d][%d]: ", i, j);
40 scanf("%d", &matrix2[i][j]);
41 }
42 }
43
44 printf("\n");
45
46 printf("\n....Your resultant matrix is....\n\n");
47
48 for(i=0;i<m1;i++)
49 {
50 for(j=0;j<n2;j++)
51 {
52 mult[i][j]=0;
53 for(k=0;k<m2;k++)
54 {
55 mult[i][j]+=matrix1[i][k]*matrix2[k][j];
56 }
57 }
58 }
59
60 for (i = 0; i < m1; i++)
61 {
62 for (j = 0; j < n2; j++)
63 {
64 printf("%d\t", mult[i][j]);
65 }
66 printf("\n");
67 }
68
69 }
70
71 else
72 printf("Matrix multiplication not possible");
73}
74
75
76
1#include <stdio.h>
2
3// function to get matrix elements entered by the user
4void getMatrixElements(int matrix[][10], int row, int column) {
5
6 printf("\nEnter elements: \n");
7
8 for (int i = 0; i < row; ++i) {
9 for (int j = 0; j < column; ++j) {
10 printf("Enter a%d%d: ", i + 1, j + 1);
11 scanf("%d", &matrix[i][j]);
12 }
13 }
14}
15
16// function to multiply two matrices
17void multiplyMatrices(int first[][10],
18 int second[][10],
19 int result[][10],
20 int r1, int c1, int r2, int c2) {
21
22 // Initializing elements of matrix mult to 0.
23 for (int i = 0; i < r1; ++i) {
24 for (int j = 0; j < c2; ++j) {
25 result[i][j] = 0;
26 }
27 }
28
29 // Multiplying first and second matrices and storing it in result
30 for (int i = 0; i < r1; ++i) {
31 for (int j = 0; j < c2; ++j) {
32 for (int k = 0; k < c1; ++k) {
33 result[i][j] += first[i][k] * second[k][j];
34 }
35 }
36 }
37}
38
39// function to display the matrix
40void display(int result[][10], int row, int column) {
41
42 printf("\nOutput Matrix:\n");
43 for (int i = 0; i < row; ++i) {
44 for (int j = 0; j < column; ++j) {
45 printf("%d ", result[i][j]);
46 if (j == column - 1)
47 printf("\n");
48 }
49 }
50}
51
52int main() {
53 int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
54 printf("Enter rows and column for the first matrix: ");
55 scanf("%d %d", &r1, &c1);
56 printf("Enter rows and column for the second matrix: ");
57 scanf("%d %d", &r2, &c2);
58
59 // Taking input until
60 // 1st matrix columns is not equal to 2nd matrix row
61 while (c1 != r2) {
62 printf("Error! Enter rows and columns again.\n");
63 printf("Enter rows and columns for the first matrix: ");
64 scanf("%d%d", &r1, &c1);
65 printf("Enter rows and columns for the second matrix: ");
66 scanf("%d%d", &r2, &c2);
67 }
68
69 // get elements of the first matrix
70 getMatrixElements(first, r1, c1);
71
72 // get elements of the second matrix
73 getMatrixElements(second, r2, c2);
74
75 // multiply two matrices.
76 multiplyMatrices(first, second, result, r1, c1, r2, c2);
77
78 // display the result
79 display(result, r1, c2);
80
81 return 0;
82}
1#include <stdio.h>
2void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2);
3void multiplyMatrices(int first[][10], int second[][10], int multResult[][10], int r1, int c1, int r2, int c2);
4void display(int mult[][10], int r1, int c2);
5
6int main() {
7 int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
8 printf("Enter rows and column for the first matrix: ");
9 scanf("%d %d", &r1, &c1);
10 printf("Enter rows and column for the second matrix: ");
11 scanf("%d %d", &r2, &c2);
12
13 // Taking input until columns of the first matrix is equal to the rows of the second matrix
14 while (c1 != r2) {
15 printf("Error! Enter rows and columns again.\n");
16 printf("Enter rows and columns for the first matrix: ");
17 scanf("%d%d", &r1, &c1);
18 printf("Enter rows and columns for the second matrix: ");
19 scanf("%d%d", &r2, &c2);
20 }
21
22 // Function to take matrices data
23 enterData(first, second, r1, c1, r2, c2);
24
25 // Function to multiply two matrices.
26 multiplyMatrices(first, second, mult, r1, c1, r2, c2);
27
28 // Function to display resultant matrix after multiplication.
29 display(mult, r1, c2);
30
31 return 0;
32}
33
34void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2) {
35 printf("\nEnter elements of matrix 1:\n");
36
37 for (int i = 0; i < r1; ++i) {
38 for (int j = 0; j < c1; ++j) {
39 printf("Enter a%d%d: ", i + 1, j + 1);
40 scanf("%d", &first[i][j]);
41 }
42 }
43 printf("\nEnter elements of matrix 2:\n");
44
45 for (int i = 0; i < r2; ++i) {
46 for (int j = 0; j < c2; ++j) {
47 printf("Enter b%d%d: ", i + 1, j + 1);
48 scanf("%d", &second[i][j]);
49 }
50 }
51}
52
53void multiplyMatrices(int first[][10], int second[][10], int mult[][10], int r1, int c1, int r2, int c2) {
54
55 // Initializing elements of matrix mult to 0.
56 for (int i = 0; i < r1; ++i) {
57 for (int j = 0; j < c2; ++j) {
58 mult[i][j] = 0;
59 }
60 }
61
62 // Multiplying first and second matrices and storing in mult.
63 for (int i = 0; i < r1; ++i) {
64 for (int j = 0; j < c2; ++j) {
65 for (int k = 0; k < c1; ++k) {
66 mult[i][j] += first[i][k] * second[k][j];
67 }
68 }
69 }
70}
71
72void display(int mult[][10], int r1, int c2) {
73
74 printf("\nOutput Matrix:\n");
75 for (int i = 0; i < r1; ++i) {
76 for (int j = 0; j < c2; ++j) {
77 printf("%d ", mult[i][j]);
78 if (j == c2 - 1)
79 printf("\n");
80 }
81 }
82}
83