Written by Sai Sandeep Thota on July 18, 2013 in Java > Programming

Java Program to Add Two Matrices

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In this article we will explain how to add two Matrices. There is a rule for Addition of Matrix, it is that the order of two Matrices must be same. Order means number of elements in rows and columns.

We are using a class named addMatrix to perform Matrix Addition. The variables m, n are row and column respectively used for Order of the matrix. We create three two-dimensional integer type variables of size specified by the user(input of m and n). Then we use a for loop and collect elements of the two matrices from the user. We again use for loop to perform addition operation and printing the third matrix, which stores the sum of two Matrices given by the user.

Java Program to Add Two Matrices

import java.util.Scanner;

class addMatrix {
	public static void main(String args[])
	{
		int m, n;
		Scanner get = new Scanner(System.in);
		System.out.print("Enter Number of Rows and Colomns:");
		m = get.nextInt();
		n = get.nextInt();
		
		int first[][] = new int[m][n];
		int second[][] = new int [m][n];
		int third[][] = new int [m][n];
		
		System.out.println("Enter First Matrix Elements:");
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				first[i][j] = get.nextInt();
			}
		}
		System.out.println("Enter Second Matrix Elements:");
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				second[i][j] = get.nextInt();
			}
		}
		
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				third[i][j] = first[i][j] + second[i][j];
			}
		}
		
		System.out.println("Sum of Two Matrices:");
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				System.out.print(third[i][j]+"\t");
			}
			System.out.println();
		}
		
	}
}	

Download Source Code

Output:
Java program to add two matrices

Java Program to Subtract Two Matrices

This is the exact replica of the above program, it just differs at one line i.e., instead of + we use - symbol in the third for loop.

import java.util.Scanner;

class subMatrix {
	public static void main(String args[])
	{
		int m, n;
		Scanner get = new Scanner(System.in);
		System.out.print("Enter Number of Rows and Colomns:");
		m = get.nextInt();
		n = get.nextInt();
		
		int first[][] = new int[m][n];
		int second[][] = new int [m][n];
		int third[][] = new int [m][n];
		
		System.out.println("Enter First Matrix Elements:");
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				first[i][j] = get.nextInt();
			}
		}
		System.out.println("Enter Second Matrix Elements:");
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				second[i][j] = get.nextInt();
			}
		}
		
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				third[i][j] = first[i][j] - second[i][j];
			}
		}
		
		System.out.println("Subtraction of Two Matrices:");
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				System.out.print(third[i][j]+"\t");
			}
			System.out.println();
		}
		
	}
}	

Download Source Code

Output:

Subraction_of_Matrices

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: