of two matrices #include <stdio.h> void main() { int mat1[10][10],mat2[10][10],resultant[10][10]; int m,n,row,col; printf(Enter the order of matrices\t); scanf(%d%d,&m,&n); printf(Enter the elments of matrix-1:); for(row=0;row<m;row++) { for(col=0;col<n;col++) scanf(%d,&mat1*row+*col]); }
printf(enter the elments of matrix-2:); for(row=0;row<m;row++) { for(col=0;col<n;col++) scanf(%d,&mat2*row+*col]); } for(row=0;row<m;row++) { for(col=0;col<n;col++) resultant[row][col]=mat1[row][col]+mat2[row][col]; } printf(The result of matrix addition is:);
Output: Enter the order of matrices 3 3 Enter the elements of matrix-1: 1 2 3 4 5 6 7 8 9 Enter the elements of matrix-2: 2 3 4 1 2 3 1 1 0
The result of matrix addition is: 3 5 7 5 7 9 8 9 9 Matrix Multiplication Program to perform multiplication of two matrices #include<stdio.h> #include<stdlib.h> main() { int mat1[10][10],mat2[10][10],resultant[10][10]={0}; int m1,n1,m2,n2,i,j,k; printf(Enter the order of matrix-1 (max.10 by 10)\t); scanf(%d%d,&m1,&n1); printf(Enter the elements of matrix-1:\n); for(i=0;i<m1;i++) { for(j=0;j<n1;j++) scanf(%d,&mat1*i][j]); }
printf(Enter the order of matrix-2(max.10 by 10)\t); scanf(%d%d,&m2,&n2); printf(Enter the elements of matrix-2:\n); for(i=0;i<m2;i++) { for(j=0;j<n2;j++) scanf(%d,&mat2*i][j]); } if(n1!=m2) { printf(Matrices are not compatible for multiplication\n); exit(1); } else { for(i=0;i<m1;i++) for(j=0;j<n2;j++) for(k=0;k<n1;k++) resultant[i][j]=[resultant[i][j]+mat1[i][k]*mat2[k][j]]; }
printf(The result of matrix multiplication is:\n); for(i=0;i<m1;i++) { for(j=0;j<n2;j++) printf(%d,resultant[i][j]); printf(\n); } }
Output: Enter the order of matrix-1 (max.10 by 10) 2 3 Enter the elements of matrix-1: 1 2 3 4 5 6 Enter the order of matrix-1 (max.10 by 10) 3 3 Enter the elements of matrix-2: 2 3 4 1 2 3 1 1 0 The result of matrix multiplication is: 7 10 10 19 28 31 Sum of Principal diagonal elements of a square matrix #include <stdio.h> void main() { int matrix[10],[10]; int order, sum=0,i,j; printf(Enter the order of square matrix); scanf(%d,&order); printf(Enter the elements of matrix); for(i=0;i<order;i++) { for(j=0;j<order;j++) { scanf(%d,&matrix[i][j]); } } for(i=0;i<order;i++) sum=sum+matrix[i][i]; printf(Sum of elements of principle diagonal is %d,sum); }
Output Enter the order of square matrix 3 Enter the elements of matrix: 1 2 3 4 5 6 7 8 9 Sum of elements of principal diagonal is 15
Transpose of a matrix #include<stdio.h> void main() { int matrix[10[10],matrix-transpose[10][10]; int m,n,i,j; printf(Enter the order of the matrix(max 10 by 10)\t); scanf(%d%d,&m,&n); printf(Enter the elements of the matrix:\n); for(i=0;i<m;i++) { for(j=o;j<n;j++) scanf(%d,&matrix[i][j]); }
for(i=0;i<n;i++) for(j=0;j<m;j++) matrix-transpose[i][j]=matrix[j][i]; printf(Transpose of the matrix is:\n); for(i=0;i<n;i++) { for(j=0;j<m;j++) printf(%d,matrix-transpose[i][j]); printf(\n); } }
Output: Enter the order of the matrix(max 10 by 10) 2 4 Enter the elements of matrix: 1 2 3 4 5 6 7 8 Transpose of the matrix is: 1 5 2 6 3 7 4 8