Contact Learn C
Copy Program Share Program

Program 336 : Subtract 2 Matrices using Pointers in C

Program 336 : Subtract 2 Matrices using Pointers in C
 
#include<stdio.h>
main()
{ 
 int i,j,rows,col;
 printf("Enter number of rows\n");
 scanf("%d",&rows);
  printf("Enter number of columns\n");
 scanf("%d",&col);
 
 int a1[rows][col],a2[rows][col],sub[rows][col];
 //Taking input for 1st matrix
 printf("Enter Matrix 1\n");
 for(i=0;i<rows;i++)
 {
  for(j=0;j<col;j++)
  {
   scanf("%d",(*(a1+i)+j));
  }
 }
  //Taking input for 2nd matrix
  printf("Enter Matrix 2\n");
 for(i=0;i<rows;i++)
 {
  for(j=0;j<col;j++)
  {
   scanf("%d",(*(a2+i)+j));
  }
 }
 //Subtraction of matrix
 for(i=0;i<rows;i++)
 {
  for(j=0;j<col;j++)
  {
   *(*(sub+i)+j)=*(*(a1+i)+j)-*(*(a2+i)+j);
  }
 }
 
 printf("Difference of above matrices(Matrix 1- Matrix 2) is\n");
 
  for(i=0;i<rows;i++)
 {
  for(j=0;j<col;j++)
  {
   printf("%d\t",*(*(sub+i)+j));
  }
  
  printf("\n");
 }
 
}



Explanation:
//Coming Soon

Output: 
Subtract 2 Matrices using Pointers in C





 

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide