Program to add two matrices

Program to add two matrices is discussed here. Two matrices are obtained as input from the user.

Addition of two matrices is possible only when both the matrices contain same number of rows and columns.

add two matrices

Algorithm to add two matrices

  • Input matrix 1 and matrix 2.
  • If the number of rows and number of columns of matr1x 1 and matrix 2 is equal,
  1. for i =1 to rows [matrix 1]  
  2. for j =1 to columns [matrix 1]  
  • Input matrix 1 [i,j]
  • Input matrix 2 [i,j]
  • matrix 3 [i, j] = matrix 1 [i, j] + matrix 2 [i, j];  

3. Display matrix 3 [i,j]; 

Program to add two matrices

// C program to add two matrices

#include
int main(){
int r, c, mat1[100][100], mat2[100][100], sum[100][100], i, j;
printf(“\nEnter the number of rows and columns : “);
scanf(“%d %d”, &r, &c);
printf(“\nInput Matrix 1 elements : “);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
scanf(“%d”,&mat1[i][j]);
}
printf(“\nMatrix 1\n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d “,mat1[i][j]);
}
printf(“\n”);
}

printf(“\nInput Matrix 2 elements : “);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
scanf(“%d”, &mat2[i][j]);
}
printf(“\nMatrix 2\n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d “,mat1[i][j]);
}
printf(“\n”);
}
// Adding Two matrices
printf(“\nAdded Matrix\n”);
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=mat1[i][j]+mat2[i][j];
}

// print the result

for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf(“%d “,sum[i][j]);

if(j==c-1)
{
printf(“\n”);
}
}

return 0;
}

// C++ program to add two matrices

#include
using namespace std;
int main(){
int r, c, mat1[100][100], mat2[100][100], sum[100][100], i, j;
cout << “\nEnter the number of rows and columns : “;
cin >> r >> c;
cout << “\nInput Matrix 1 elements : “;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cin >> mat1[i][j];
}
cout << “\nMatrix 1\n”;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout << mat1[i][j]<< ” “;
}
cout << “\n”;
}

cout << “\nInput Matrix 2 elements : “;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cin >> mat2[i][j];
}
cout << “\nMatrix 2\n”;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout << mat1[i][j]<< ” “;
}
cout << “\n”;
}
// Adding Two matrices
cout << “\nAdded Matrix\n”;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=mat1[i][j]+mat2[i][j];
}

// print the result

for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout <<sum[i][j] << ” “;

if(j==c-1)
{
cout << “\n”;
}
}

return 0;
}

// Java program to add two matrices

import java.util.*;

class Main
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);

System.out.println(“Enter the number of rows and columns of matrix”);
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];

System.out.println(“Enter the elements of first matrix”);

for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
first[c][d] = in.nextInt();

System.out.println(“Enter the elements of second matrix”);

for (c = 0 ; c < m ; c++)
for (d = 0 ; d < n ; d++)
second[c][d] = in.nextInt();

for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d]; //replace ‘+’ with ‘-‘ to subtract matrices

System.out.println(“Sum of the matrices:”);

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
System.out.print(sum[c][d]+”\t”);

System.out.println();
}
}
}

# Python program to add two matrices

N = 3
# Fucntion to perform addition of matrices
def add(A,B,C):

for i in range(N):
for j in range(N):
C[i][j] = A[i][j] + B[i][j]

A = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

B = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

C=A[:][:] # To store the result

add(A, B, C)

print(“Result matrix is”)
for i in range(N):
for j in range(N):
print(C[i][j], ” “, end=”)
print()

#include<stdio.h>
#include<stdlib.h>
int i,j;
int** memory(int c,int r){
int **a;
a=(int **)malloc(c*sizeof(int*));
for(i=0;i<c;i++)
*(a+i)=(int *)malloc(r*sizeof(int));
return a;
}
int main()
{
int **a,**b,r,c;
scanf(“%d %d”,&r,&c);
a=memory(c,r);
b=memory(c,r);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf(“%d”,&a[i][j]);
}
}

for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf(“%d”,&b[i][j]);
}
}for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf(“%d “,a[i][j]+b[i][j]);
}printf(“\n”);
}
return 0;
}

Output:

add two matrices

Time complexity: O(n^2)