You are on page 1of 8

ASSIGNMENT-4

PROBLEM STATEMENT-:
write a menu driven program in c perform
matrix addition and subtraction of 2 matrices

Implementation-:
CODING-
#include <stdio.h>

int main()
{
int a[2][2],b[2][2],c[2][2],d[2][2],choice;
int i,j;
printf("---Matrix addition and
subraction---");

printf("\n Enter the Element of First


Matrix ::");
for ( i = 0; i < 2; i++)
{
for(j=0;j<2;j++)
{
printf("elements of first matrix a
[%d][%d]",i,j);
scanf("%d",&a[i][j]);
}
}
printf("This is your First matrix::");
for ( i = 0; i < 2; i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\n Enter the Element of Second
Matrix ::");
for ( i = 0; i < 2; i++)
{
for(j=0;j<2;j++)
{
printf("elements of second matrix b
[%d][%d]",i,j);
scanf("%d",&b[i][j]);
}
}
printf("This is your Second matrix::");
for ( i = 0; i < 2; i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d\t",b[i][j]);
}
}
printf("\nPress 1 for add\n");
printf("Press 2 for sub\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\n addition result");
for ( i = 0; i < 2; i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n This is your Addition
Resultant matrix::");
for ( i = 0; i < 2; i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d\t",c[i][j]);
}
}
break;
}
case 2:
{
printf("\n subtraction result");
for ( i = 0; i < 2; i++)
{
for(j=0;j<2;j++)
{
d[i][j]=a[i][j]-b[i][j];
}
}
printf("\n This is your Subtraction
Resultant Matrix::");
for ( i = 0; i < 2; i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d\t",d[i][j]);
}
}
break;
}
}
}
RESULTS-
---Matrix addition and subraction---
Enter the Element of First Matrix ::
elements of first matrix a [0][0]1
elements of first matrix a [0][1]2
elements of first matrix a [1][0]4 3
elements of first matrix a [1][1]4
This is your First matrix::
1 2
3 4
Enter the Element of Second Matrix ::
elements of second matrix b [0][0]5
elements of second matrix b [0][1]6
elements of second matrix b [1][0]7
elements of second matrix b [1][1]8
This is your Second matrix::
5 6
7 8
Press 1 for add
Press 2 for sub
2

subtraction result
This is your Subtraction Resultant
Matrix::
-4 -4
-4 -4

You might also like