Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday, 6 November 2017

Java Program to add 2 matrices of size 3

import java.util.Scanner;

public class Addition
{
   public static void main(String args[])
   {
       int i, j;
       int matrix1[][] = new int[3][3];
       int matrix2[][] = new int[3][3];
       int matrix3[][] = new int[3][3];
       Scanner scan = new Scanner(System.in);
    
       System.out.print("Enter Matrix  Elements : ");
       for(i=0; i<3; i++)
       {
           for(j=0; j<3; j++)
           {
               matrix1[i][j] = scan.nextInt();
           }
       }
    
       System.out.print("Enter Matrix  Elements : ");
       for(i=0; i<3; i++)
       {
           for(j=0; j<3; j++)
           {
               matrix2[i][j] = scan.nextInt();
           }
       }
    
       System.out.print("Adding both Matrix to form the Third Matrix...\n");
       for(i=0; i<3; i++)
       {
           for(j=0; j<3; j++)
           {
               matrix3[i][j] = matrix1[i][j] + matrix2[i][j];
           }
       }
        System.out.print("Matrix after addition will be :\n");
       for(i=0; i<3; i++)
       {
           for(j=0; j<3; j++)
           {
               System.out.print(matrix3[i][j]+ " ");
           }
           System.out.println();
       }
   }
}

No comments:

Post a Comment