Matrix subtraction C program

It’s a matrix subtraction C program that inputs numbers from the user and stores these numbers in two different two-dimensional arrays and then subtracts them, and print result on the screen.

To understand this program, it’s better you should know the following topics:

Program (1): to find the subtraction of two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
//consider both matrices A and B size as 2x2
int a[2][2],b[2][2],c[2][2];
int i,j;
//accepting values into an array a
printf("Accept elements in Matrix A\n");
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
      {
      printf("Enter element :- ",i+1);
       scanf("%d",&a[i][j]);
       }
 }
//accepting values into an array b
printf("Accept elements in Matrix B\n");
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
      {
      printf("Enter element :- ",i+1);
       scanf("%d",&b[i][j]);
       }
 }
//to find addition of  array a and b elements and print array c elements
printf("\nThe resultant matrix is\n");
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
      {
       c[i][j]=a[i][j]-b[i][j];
       printf("%6d",c[i][j]);
       }
       printf("\n");
 }
getch();
}

Output (1)

Accept elements in Matrix A
Enter element :- 1
Enter element :- 2
Enter element :- 3
Enter element :- 4
Accept elements in Matrix B
Enter element :- 1
Enter element :- 3
Enter element :- 5
Enter element :- 5

The resultant matrix is
     0    -1
    -2    -1

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *