Program to Add two Matrices

Description

* To add two matrices, the row and column of first matrix should be equal to row 
  and column of second matrix respectively. 
* Addition is performed between corresponding elements of both the matrices.
* Resultant matrix have the same number of rows and column as the matrix to add.
* To add two matrices,
  the row and column of first matrix should be equal to row 
  and column of second matrix respectively. 
* Addition is performed between corresponding elements of both the matrices.
* Resultant matrix have the same number of rows and column as the matrix to add.

C/C++

/* C program to add two matrices */
//Save it as AddTwoMatrix.c

#include<stdio.h>
int main(){

    int i, j, row1, column1, row2, column2;

    printf("Enter the number of rows of first matrix : ");
    scanf("%d",&row1);

    printf("Enter the number of columns of first matrix : ");
    scanf("%d",&column1);

    //Declaring first matrix
    int matrix1[row1][column1];

    //Reading input for first matrix
    printf("Enter the elements of first matrix : \n");
    for(i=0;i<row1;i++) {
        for(j=0;j<column1;j++) {
            printf("matrix1[%d][%d] : ",i,j);
            scanf("%d",&matrix1[i][j]);
        }
    }

    //Displaying Output for first matrix
    printf("The elements of first matrix : \n");
    for(i=0;i<row1;i++) {
        for(j=0;j<column1;j++) {
            printf("%d ",matrix1[i][j]);
        }
        printf("\n");
    }

    printf("Enter the number of rows of second matrix : ");
    scanf("%d",&row2);

    printf("Enter the number of columns of second matrix : ");
    scanf("%d",&column2);
    
    //Declaring second matrix
    int matrix2[row2][column2];

    //Reading input for second matrix
    printf("Enter the elements of second matrix : \n");
    for(i=0;i<row2;i++) {
        for(j=0;j<column2;j++) {
            printf("matrix2[%d][%d] : ",i,j);
            scanf("%d",&matrix2[i][j]);
        }
    }

    //Displaying Output for second matrix
    printf("The elements of second matrix : \n");
    for(i=0;i<row2;i++) {
        for(j=0;j<column2;j++) {
            printf("%d ",matrix2[i][j]);
        }
        printf("\n");
    }
    //Declaring Result matrix after addition
    int matrix3[row1][column1];

    if(row1!=row2 || column1!=column2){
        printf("\nMatrix can not be added");
        printf("\nTo add two matrix");
        printf("the number of rows and column of first matrix should be");
        printf(" equal to rows and column of second matrix");
    }
    else{
        for(i=0;i<row1;i++){
            for(j=0;j<column1;j++){
               //Adding two matrix and storing result into resultant matrix
               matrix3[i][j] = (matrix1[i][j] + matrix2[i][j]);
            }
        }
        //Displaying Output after addition
        printf("The matrix after addition is : \n");
        for(i=0;i<row1;i++) {
            for(j=0;j<column1;j++) {
                printf("%d ",matrix3[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
Input:
Enter the number of rows of first matrix : 2
Enter the number of columns of first matrix : 2
Enter the elements of first matrix :
matrix1[0][0] : 1
matrix1[0][1] : 2
matrix1[1][0] : 3
matrix1[1][1] : 4

Enter the number of rows of second matrix : 2
Enter the number of columns of second matrix : 2
Enter the elements of second matrix :
matrix2[0][0] : 1
matrix2[0][1] : 2
matrix2[1][0] : 3
matrix2[1][1] : 4

Output:
The elements of first matrix :
1 2
3 4
The elements of second matrix :
1 2
3 4
The matrix after addition is :
2 4
6 8

Java

/* Java program to add two matrices */
//Save it as AddTwoMatrix.java

import java.io.*;
import java.util.Scanner;

public class AddTwoMatrix {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        int i, j, row1, column1, row2, column2;

        System.out.println("Enter the number of rows of first matrix : ");
        row1 = scanner.nextInt();

        System.out.println("Enter the number of columns of first matrix : ");
        column1 = scanner.nextInt();

        //Declaring first matrix
        int matrix1[][] = new int[row1][column1];

        //Reading input for first matrix
        for (i = 0; i < row1; i++) {
            for (j = 0; j < column1; j++) {
                System.out.println("matrix1[" + i + "]" + "[" + j + "] : ");
                matrix1[i][j] = scanner.nextInt();
            }
        }

        //Displaying Output for first matrix
        System.out.println("The first matrix is : ");
        for (i = 0; i < row1; i++) {
            for (j = 0; j < column1; j++) {
                System.out.print(matrix1[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("Enter the number of rows of second matrix : ");
        row2 = scanner.nextInt();

        System.out.println("Enter the number of columns of second matrix : ");
        column2 = scanner.nextInt();

        //Declaring Second matrix
        int matrix2[][] = new int[row2][column2];

        //Reading input for second matrix
        System.out.println("Enter the elements of second matrix : ");
        for (i = 0; i < row2; i++) {
            for (j = 0; j < column2; j++) {
                System.out.println("matrix2[" + i + "]" + "[" + j + "] : ");
                matrix2[i][j] = scanner.nextInt();
            }
        }
        //Displaying Output for second matrix
        System.out.println("The second matrix is : ");
        for (i = 0; i < row2; i++) {
            for (j = 0; j < column2; j++) {
                System.out.print(matrix2[i][j] + " ");
            }
            System.out.println();
        }

        //Declaring Result matrix after addition
        int matrix3[][] = new int[row1][column1];

        if(row1!=row2 || column1!=column2){
            System.out.println("Matrix can not be added");
            System.out.print("To add two matrix");
            System.out.print(" the number of rows and column of first matrix should be");
            System.out.print(" equal to rows and column of second matrix");
        }
        else{
            for(i=0;i<row1;i++){
                    for(j=0;j<column1;j++){
                   //Adding two matrix and storing result into resultant matrix
                   matrix3[i][j] = (matrix1[i][j] + matrix2[i][j]);
                }
            }
            //Displaying Output after addition
            System.out.println("The matrix after addition is : ");
            for (i = 0; i < row1; i++) {
                    for (j = 0; j < column1; j++) {
                        System.out.print(matrix3[i][j] + " ");
                    }
                    System.out.println();
                }
        }
    }
}
Input:
Enter the number of rows of first matrix : 
2
Enter the number of columns of first matrix : 
2
matrix1[0][0] : 
1
matrix1[0][1] : 
2
matrix1[1][0] : 
3
matrix1[1][1] : 
4 
Enter the number of rows of second matrix : 
2
Enter the number of columns of second matrix : 
2
Enter the elements of second matrix : 
matrix2[0][0] : 
1
matrix2[0][1] : 
2
matrix2[1][0] : 
3
matrix2[1][1] : 
4
Output:
The first matrix is : 
1 2 
3 4
The second matrix is : 
1 2 
3 4 
The matrix after addition is : 
2 4 
6 8

Related Programs

1) Program to read and display 2D array
2) Program to Transpose a matrix
3) Program to Multiply two Matrices
4) Program to Find the smallest missing number
5) Program to Search for an Element in an Array
6) Program to Rearrange an array such that arr[i]=i
7) Program to Find missing odd number in first n odd number
8) Program to find maximum and second maximum if elements of array is space separated input
9) Program to Print How Many Numbers Smaller than Current Number
10) Remove Duplicate Elements From Array
Share Me

Leave a Reply