recent

C Program To Evaluate Subtraction Of Two Matrix(2D-Array)

C Program To Evaluate Subtraction Of Two Matrix(2D-Array)

#include<stdio.h>

int main()
 {

   int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
   int row1, col1, row2, col2;

   printf("Enter the number of Rows of Mat1 : \n");
   scanf("%d", &row1);
 
   printf("Enter the number of Cols of Mat1 :\n ");
   scanf("%d", &col1);

   printf("Enter the number of Rows of Mat2 :\n ");
   scanf("%d", &row2);
 
   printf("Enter the number of Columns of Mat2 : \n");
   scanf("%d", &col2);

//check row and col. equal or not
   if (row1 != row2 || col1 != col2) {
      printf("Order of two matrices is not same\n");
      exit(0);
   }

   //Elements in Matrix 1
   for (i = 0; i < row1; i++)
   {
      for (j = 0; j < col1; j++)
 {
         printf("Enter the Element a[%d][%d] : ", i, j);
         scanf("%d", &mat1[i][j]);
      }
   }

   //Elements in Matrix 2
   for (i = 0; i < row2; i++)
   {
      for (j = 0; j < col2; j++)
  {
         printf("Enter the Element b[%d][%d] : ", i, j);
         scanf("%d", &mat2[i][j]);
      }
   }
   //Subtraction of two matrices
   for (i = 0; i < row1; i++)
      for (j = 0; j < col1; j++) {
         mat3[i][j] = mat1[i][j] - mat2[i][j];
      }

   //Print out the Resultant Matrix
   printf("The Subtraction of two Matrices is :\n");
   for (i = 0; i < row1; i++)
   {
      for (j = 0; j < col1; j++)
 {
         printf("%d\t", mat3[i][j]);
      }
      printf("\n");
   }
}
Powered by Blogger.