0% found this document useful (0 votes)
117 views

Addition of Matrices

Uploaded by

sanjayyalla4661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Addition of Matrices

Uploaded by

sanjayyalla4661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exp.

Name: Write a C program to find the Addition of Two matrices by checking


S.No: 23 Date: 2022-03-12
compatibility

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:

Enter the row & column sizes of matrix-1 :

For example, if the user gives the input as:

Enter the row & column sizes of matrix-1 : 2 2

Next, the program should print the message on the console as:

Enter matrix-1 4 elements :

if the user gives the input as:

Enter matrix-1 4 elements : 1 2 3 4

Next, the program should print the message on the console as:

Enter the row & column sizes of matrix-2 :

if the user gives the input as:

Enter the row & column sizes of matrix-2 : 2 2

Next, the program should print the message on the console as:

Enter matrix-2 4 elements :

2021-2025-IT
if the user gives the input as:

Enter matrix-2 4 elements : 1 2 3 4

then the program should print the result as:

The given matrix-1 is Sasi Institute of Technology and Engineering (Autonomous)


1 2
3 4
The given matrix-2 is
1 2
3 4
Addition of two matrices is
2 4
6 8

Note 1: Do use the printf() function with a newline character ( \n ).


Note 2: If the sizes of two matrices are not equal then print Addition is not possible and stop the
process.

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]);
}
}

printf("The given matrix-1 is\n");


for(i=0;i<rows_1;i++)
{
for(j=0;j<coloumns_1;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");
}

Execution Results - All test cases have succeeded!

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

Sasi Institute of Technology and Engineering (Autonomous)


Test Case - 2

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy