Q. Write an algorithm and program to add two matrices.

Solution :In this program, you'll learn to add two matrices using Nested loop and display it using Nested loop.

Matrices:- A two dimensional(2D) array is known as Matrix. It can have m number of rows and n number of columns where m and n are two integer values.

Matrix Addition algorithm

//Algorithm to Add Two Matrices.

START
  Step 1 ->  Input matrix 1 and matrix 2.
  Step 2 ->  If the number of rows and number of columns of matrix 1 and matrix 2 are equal then execute step 3 else addition not possible
  Step 3 ->  for i=1 to rows[matrix 1]
                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];
  step 4->   Display matrix 3 [i,j];
STOP


Matrices Addition Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //Program to Add Two Matrices In C.
    
    #include <stdio.h>
    
    int main()
    {
        int mat1[2][2], mat2[2][2], sum[2][2], i, j;
        for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            {
             scanf("%d",&mat1[i][j]);
             scanf("%d",&mat2[i][j]);
             sum[i][j]=mat1[i][j]+mat2[i][j];
            }
        }
        for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            {printf("%d\t",sum[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }
    
    //Program to Add Two Matrices In C++.
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int mat1[2][2], mat2[2][2], sum[2][2], i, j;
        for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            {
             cin>>mat1[i][j];
             cin>>mat2[i][j];
             sum[i][j]=mat1[i][j]+mat2[i][j];
            }
        }
        for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            {cout<<sum[i][j]<<"\t";
            }
            printf("\n");
        }
    
        return 0;
    }
    
    //Program to Add Two Matrices In Java.
    
    import java.util.*;
    public class Main
    {
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
            int mat1[][] = new int[2][2];
            int mat2[][] = new int[2][2];
            int sum[][] = new int[2][2];
            int i,j;
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    mat1[i][j] = in.nextInt();
                    mat2[i][j] = in.nextInt();
                    sum[i][j]=mat1[i][j]+mat2[i][j];
                }
            }
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    System.out.print(sum[i][j]+"\t");
                }
                System.out.println();
            }
            
            
      }
    }
    
    //Program to Add Two Matrices In Python.
     
    mat1 = [[1,3],
        [5,7]]
    
    mat2 = [[2,4],
        [6,8]]
    
    sum = [[0,0],
        [0,0]]
    
    
    for i in range(len(mat1)):
      
       for j in range(len(mat1[0])):
           sum[i][j] = mat1[i][j] + mat2[i][j]
    
    for r in sum:
       print(r)
      
    //Program to Add Two Matrices In C#.
    
    using System;
    public class Exercise19 {
       public static void Main() {
          int i, j;
          int[, ] mat1 = new int[2, 2];
          int[, ] mat2 = new int[2, 2];
          int[, ] sum = new int[2, 2];
          
    
         
          for (i = 0; i < 2; i++) {
             for (j = 0; j < 2; j++)
             {
                mat1[i, j] = Convert.ToInt32(Console.ReadLine());
                mat2[i, j] = Convert.ToInt32(Console.ReadLine());
                
             }
          }
          
          for (i = 0; i < 2; i++)
             for (j = 0; j < 2; j++)
                sum[i, j] = mat1[i, j] + mat2[i, j];
          
          for (i = 0; i < 2; i++) {
             Console.Write("\n");
             for (j = 0; j < 2; j++)
                Console.Write("{0}\t", sum[i, j]);
          }
          Console.Write("\n\n");
       }
    }
    //Program to Add Two Matrices In PHP.
    
    <?php
    $mat1=array(array());
    $mat2=array(array());
    $sum=array(array());
    $m=1;
    $n=2;
    for($i=0;$i<2;$i=$i+1)
    {
        for($j=0;$j<2;$j=$j+1)
        {
            $mat1[$i][$j]=$m;
            echo $mat1[$i][$j]." ";
            $m=$m+2;
             $mat2[$i][$j]=$n;
            echo $mat2[$i][$j]." ";
            $n=$n+2;
        }
    
    }
    echo"<br/>";
    for($i=0;$i<2;$i=$i+1)
    {
        for($j=0;$j<2;$j=$j+1)
        {
            $sum[$i][$j]=$mat1[$i][$j]+$mat2[$i][$j]; 
            echo $sum[$i][$j]." ";
        }
      
    }
    ?>
    //Program to Add Two Matrices In C.
    
    #include <stdio.h>
    
    int main()
    {
        int mat1[2][2], mat2[2][2], sum[2][2], i, j;
        for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            {
             scanf("%d",&mat1[i][j]);
             scanf("%d",&mat2[i][j]);
             sum[i][j]=mat1[i][j]+mat2[i][j];
            }
        }
        for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            {printf("%d\t",sum[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }
    

    Output

    mat1   mat2
    1      2
    3      4
    5      6
    7      8
    
    sum matrix :
    3   7
    11  15
    

    Recommended Programs

       Program to check whether string is substring of another or not.
       Program to find sum of elements in a given array.