Java program to add two matrix

      No Comments on Java program to add two matrix

Add two matrix

This program illustrate the concept to add two matrix

  • Here in this program “i” is number of rows and “j” is number of coloumns.
  • “a[3][3]” is the name of first matrix with 3 rows and 3 coloumns .
  • “b[3][3]” is the name of second matrix with 3 rows and 3 coloumns .
  • “c[3][3]” is the name of resultant matrix with 3 rows and 3 coloumns .In this array result will be stored.
import java.util.*;
class scan
{
	public static void main(String arg[])
	{
 int a[][]=new int[3][3];
 int b[][]=new int[3][3];
 int c[][]=new int[3][3];
 int i,j;
 Scanner kb=new Scanner(System.in);
 System.out.println("Enter First Matrice row wise:-\n");
 
 for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     a[i][j]=kb.nextInt();
    }
  }
 
  System.out.println("Enter Second Matrice row wise:-\n");
 
 for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     b[i][j]=kb.nextInt();
    }
  }
 
 
 
 for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     c[i][j]=a[i][j]+b[i][j];
    }
  }
 
  System.out.println("Sum of Matrice is:-");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print(" "+c[i][j]);
 
    }
    System.out.println();
  }
}
}

OUTPUT:-

Add two matrix


Comment below if you are facing any problem


Also read

java program to print a matrix in 2d array
Java program to transpose a matrix


Summary
Java program to add two matrix
Article Name
Java program to add two matrix
Description
This program illustrate the concept to add two matrix.Here in this program “i” is number of rows and “j” is number of coloumns.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *