16. Programs to multiply two Matrices
# include <stdio.h> # include <conio.h> // Linux user - Remove this int main( ) { // Declaring variable a, b, c with max size=10 X 10 int a[10][10],b[10][10],c[10][10]; // Declaring variable m, n, p, q = size of matrix entered by user int m, n, p, q; // Declaring variable i, j, k = to iterate loop int i, j, k; // Inputing size of first matrix printf("Enter the size of first matrices : \n"); printf("Enter number of rows : "); scanf("%d",&m); printf("Enter number of columns : "); scanf("%d",&n); // Inputing size of second matrix printf("Enter the size of second matrices : \n"); printf("Enter number of rows : "); scanf("%d",&p); printf("Enter number of columns : "); scanf("%d",&q); // Checking if multiplication is possible or not if possible then proceed if(n==p) { // Inputing first matrx elemets printf("Enter first matrices elements : "); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } // Inputing second matrx elemets printf("Enter second matrix elements : "); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } } // Performing multiplication for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } // Printing multiplied matrix printf("The multiplication of Matrices : \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d ",c[i][j]); } printf("\n"); } } else printf("Multiplication is not possible"); getch(); // Linux user - Remove this return 0; }
17. Program to print a diagonal matrix with diagonal value enter by user
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variale a with size = 4 X 4 int a[4][4],i,j; // Declaring varibale d = diagonal value by user int d; // Inputing diagonal value printf("Enter diagonal value : "); scanf("%d",&d); // Setting diagonal value as "d" and all other value as "0" for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(i==j) a[i][j]=d; else a[i][j]=0; } } // Printing diagonal matrix for(i=0;i<4;i++) { for(j=0;j<4;j++) printf("%2d",a[i][j]); printf("\n"); } getch(); // Linux user - Remove this return 0; }
18. Program to print a anti diagonal matrix with diagonal value enter by user
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variale a with size = 4 X 4 int a[4][4],i,j; // Declaring varibale d = diagonal value by user int d; // Inputing diagonal value printf("Enter diagonal value : "); scanf("%d",&d); // Setting diagonal value as "d" and all other value as "0" for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(i+j==3) a[i][j]=d; else a[i][j]=0; } } // Printing anti diagonal matrix for(i=0;i<4;i++) { for(j=0;j<4;j++) printf("%2d",a[i][j]); printf("\n"); } getch(); // Linux user - Remove this return 0; }
19. Program to print the sum of diagonal values and anti-diagonal values of a matrix
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring 2D array int a[4][4]; // Declaring variable i, j to iterate loop int i, j; // Declaring variabl sd=to store sum of diagonal value, sa=store sum of anti diagonal value int sd=0, sa=0; // Inputing matrix printf("Enter matrix element of size 4 X 4 : "); for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&a[i][j]); } } // Calculating sum of diagonal values and anti diagonal values for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(i==j) sd=sd+a[i][j]; if(i+j==3) sa=sa+a[i][j]; } } // Printing element of matrix printf("\nEntered elements in matrix : \n"); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf("%d ",a[i][j]); } printf("\n"); } // Printing sum and diagonal of matrix printf("\nSum of diagonal values : %d ",sd); printf("\nSum of anti diagonal values : %d ",sa); getch(); // Linux user - Remove this return 0; }