Program to find addition of two matrices

Problem Statement:- Program to Find the addition of two matrices.

Sample Input/Output:-


Sample Input First: 2 2

4 5 6 7


1 2 3 8

Sample Output First: 

5 7 9 15

Sample Input Second:3 3 

4 5 6 7 8 9 2 3 4


10 11 12 13 14 15 16 17 18

Sample Output Second: 

14 16 18 20 22 24 18 20 22


Explanation:- 


For 1st output: { matrix[0][0]+matrix1[0][0] } { matrix[0][1]+matrix1[0][1] } { matrix[1][0]+matrix1[1][0] } { matrix[1][1]+matrix1[1][1] }={ 4+1 } {5+2 } {6+3 }.{ 7+8}=5 7 9 15


For 2nd output: { matrix[0][0]+matrix1[0][0] } { matrix[0][1]+matrix1[0][1] }........{ matrix[2][2]+matrix1[2][2] }={ 4+10 } {5+11 }.............{ 4+18 }=14 16...........22

Data requirement:-


  Input Data:- row_size, col_size, matrix[][], matrix1[][]


  Output Data:-sum_matrix[][]


  Additional Data:- i, j


Program in C

Here is the source code of the C Program to Program to find the addition of two matrices.


Code:


#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } //Calculate sum of given matrix Elements int sum=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sum+=matrix[i][j]; } } //Display The Sum Of Given Matrix Elements printf("Sum of the Given Matrix Elements is: %d",sum); }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the 1st Matrix Element: 4 5 6 7 Enter the 2nd Matrix Element: 1 2 3 8 1st Matrix is: 4 5 6 7 2nd Matrix is: 1 2 3 8 Sum of the two Matrices is: 5 7 9 15


Program in C++

Here is the source code of the C++ Program to perform the addition of two 3*3 matrices.


Code:


#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the 1st matrix int i,j; cout<<"Enter the 1st Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } int matrix1[row_size][col_size]; //Taking input of the 2nd matrix cout<<"Enter the 2nd Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix1[i][j]; } } int sum_matrix[row_size][col_size]; //Compute Addition of two matrices for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sum_matrix[i][j]=matrix[i][j]+matrix1[i][j]; } } //display the 1st Matrix cout<<"1st Matrix is:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cout<<matrix[i][j]<<" "; } cout<<"\n"; } //display the 2nd Matrix cout<<"2nd Matrix is:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cout<<matrix1[i][j]<<" "; } cout<<"\n"; } //display the sum of two matrices cout<<"Sum of the two Matrices is:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cout<<sum_matrix[i][j]<<" "; } printf("\n"); } }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the 1st Matrix Element: 4 5 6 7 8 9 2 3 4 Enter the 2nd Matrix Element: 10 11 12 13 14 15 16 17 18 1st Matrix is: 4 5 6 7 8 9 2 3 4 2nd Matrix is: 10 11 12 13 14 15 16 17 18 Sum of the two Matrices is: 14 16 18 20 22 24 18 20 22


Program in Java

Here is the source code of the Java Program to Program to find the sum of two matrices.


Code:


import java.util.Scanner; public class SumOfTwoMatrix { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the 1st matrix int i,j; System.out.println("Enter the 1st Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } int matrix1[][] =new int[row_size][col_size]; //Taking input of 2nd the matrix System.out.println("Enter the 2nd Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix1[i][j]=cs.nextInt(); } } int sum_matrix[][] =new int[row_size][col_size]; //Compute Addition of two matrices for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sum_matrix[i][j]=matrix[i][j]+matrix1[i][j]; } } //display the 1st Matrix System.out.println("Given 1st Matrix is:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { System.out.print(matrix[i][j]+" "); } System.out.println(); } //display the 2nd Matrix System.out.println("Given 2nd Matrix is:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { System.out.print(matrix1[i][j]+" "); } System.out.println(); } //display the sum of two matrices System.out.println("Sum of the two Matrices is:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { System.out.print(sum_matrix[i][j]+" "); } System.out.println(); } cs.close(); } }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:3 Enter the 1st Matrix Element: 2 3 5 6 7 8 Enter the 2nd Matrix Element: 20 30 33 34 77 21 Given 1st Matrix is: 2 3 5 6 7 8 Given 2nd Matrix is: 20 30 33 34 77 21 Sum of the two Matrices is: 22 33 38 40 84 29



Program in Python

Here is the source code of the Python Program to find the addition of two matrices.


Code:


# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the 1st matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) matrix1=[] # Taking input of the 2nd matrix print("Enter the Matrix Element:") for i in range(row_size): matrix1.append([int(j) for j in input().split()]) # Compute Addition of two matrices sum_matrix=[[0 for i in range(col_size)] for i in range(row_size)] for i in range(len(matrix)): for j in range(len(matrix[0])): sum_matrix[i][j]=matrix[i][j]+matrix1[i][j] # display the sum of two matrices print("Sum of the two Matrices is:") for m in sum_matrix: print(m)


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:4 Enter the Matrix Element: 1 2 3 4 5 6 7 8 Enter the Matrix Element: 8 7 6 5 4 3 2 1 Sum of the two Matrices is: [9, 9, 9, 9] [9, 9, 9, 9]


Most Recommend Questions:-








More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question


Post a Comment

0 Comments