Addition of Matrices
Addition of Matrices
Page No:
Aim:
Write a program to find the addition of two matrices .
ID: 21K61A1240
At the time of execution, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
2021-2025-IT
if the user gives the input as:
Source Code:
Program511.c
#include<stdio.h>
Page No:
void main()
{
int arr[10][10],brr[10][10],crr[10][10],rows_1,coloumns_1,rows_2,coloumns_2,i,j;
printf("Enter the row & column sizes of matrix-1 : ");
ID: 21K61A1240
scanf("%d%d",&rows_1,&coloumns_1);
printf("Enter matrix-1 %d elements : ",rows_1*coloumns_1);
for(i=0;i<rows_1;i++)
{
for(j=0;j<coloumns_1;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("Enter the row & column sizes of matrix-2 : ");
scanf("%d%d",&rows_2,&coloumns_2);
printf("Enter matrix-2 %d elements : ",rows_2*coloumns_2);
for(i=0;i<rows_2;i++)
{
for(j=0;j<coloumns_2;j++)
{
scanf("%d",&brr[i][j]);
}
}
2021-2025-IT
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("The given matrix-2 is\n"); Sasi Institute of Technology and Engineering (Autonomous)
for(i=0;i<rows_2;i++)
{
for(j=0;j<coloumns_2;j++)
{
printf("%d ",brr[i][j]);
}
printf("\n");
}
if(rows_1==rows_2&&coloumns_1==coloumns_2)
{
for(i=0;i<rows_1;i++)
{
for(j=0;j<coloumns_1;j++)
{
crr[i][j]=arr[i][j]+brr[i][j];
}
}
printf("Addition of two matrices is\n");
for(i=0;i<rows_1;i++)
Page No:
{
for(j=0;j<coloumns_1;j++)
{
ID: 21K61A1240
printf("%d ",crr[i][j]);
}
printf("\n");
}
}
else
printf("Addition is not possible\n");
}
Test Case - 1
User Output
Enter the row & column sizes of matrix-1 : 2 2
Enter matrix-1 4 elements : 11 22 33 44
Enter the row & column sizes of matrix-2 : 2 2
Enter matrix-2 4 elements : 22 33 44 55
The given matrix-1 is
11 22
33 44
The given matrix-2 is
22 33
2021-2025-IT
44 55
Addition of two matrices is
33 55
77 99
User Output
Enter the row & column sizes of matrix-1 : 2 3
Enter matrix-1 6 elements : 1 2 3 4 5 6
Enter the row & column sizes of matrix-2 : 3 2
Enter matrix-2 6 elements : 1 3 4 5 6 7
The given matrix-1 is
1 2 3
4 5 6
The given matrix-2 is
1 3
4 5
6 7
Addition is not possible
Test Case - 3
User Output
Page No:
Enter the row & column sizes of matrix-1 : 3 3
Enter matrix-1 9 elements : 1 2 3 4 5 6 7 8 9
Enter the row & column sizes of matrix-2 : 3 3
Enter matrix-2 9 elements : 1 3 2 4 6 5 8 7 9
ID: 21K61A1240
The given matrix-1 is
1 2 3
4 5 6
7 8 9
The given matrix-2 is
1 3 2
4 6 5
8 7 9
Addition of two matrices is
2 5 5
8 11 11
15 15 18
2021-2025-IT
Sasi Institute of Technology and Engineering (Autonomous)