javatutelearn.com

Easy Learning


Home

Search Javatutelearn.com :


Matrix Example using java array

Example 1: Matrix input and output

Following example shows inputting of matrix and displaying matrix values
import java.util.Scanner; 
public class mat1 {
	public static void main (String args[])
	{
		int mat [][]; // No memory allocation
		int i,j,num;
		mat = new int [3][3]; // memory is allocated to array
		Scanner sc = new Scanner(System.in); 
		// Taking values from user
		System.out.println("Enter Matrix (3 × 3) values: ");
		for(i=0;i<3;i++)
		{
			for (j=0;j<3;j++)
			{
				num = sc.nextInt();
				mat[i][j] = num;	
			}
			
		}
		// Displaying Matrix values
		System.out.println("Matrix is:\n");
		for (i=0;i<3;i++)
		{
		  for(j=0;j<3;j++)	
		  {
			System.out.print(" "+ mat[i][j]);
		  }
		  System.out.println("");
		}
	}
}

Output

Enter Matrix (3 × 3) values: 
2 3 4
3 4 5
6 7 8

Matrix is:
2 3 4
3 4 5
6 7 8

Example 2: Addition of two matrices (3 × 3)

import java.util.Scanner; 
public class addmatrix {
	public static void main (String args[])
	{
		int[][] mat1,mat2,result; // No memory allocation
		int i,j,num;
		mat1 = new int [3][3]; // memory is allocated to array
		mat2 = new int [3][3]; 
		result = new int [3][3]; 
		Scanner sc = new Scanner(System.in); 
		// Taking values from user
		System.out.println("Enter First Matrix (3 × 3) values: ");
		for(i=0;i<3;i++)
		{
			for (j=0;j<3;j++)
			{
				num = sc.nextInt();
				mat1[i][j] = num;	
			}
			
		}
		System.out.println("Enter Second Matrix (3 × 3) values: ");
		for(i=0;i<3;i++)
		{
			for (j=0;j<3;j++)
			{
				num = sc.nextInt();
				mat2[i][j] = num;	
			}
			
		}
		// Addition of two matrices
		for(i=0;i<3;i++)
		{
			for (j=0;j<3;j++)
			{
				result[i][j] = mat1[i][j]+mat2[i][j];	
			}
			
		}
		// Displaying addition of two matrices 
		System.out.println("Addition two matrices is:\n");
		for (i=0;i<3;i++)
		{
		  for(j=0;j<3;j++)	
		  {
			System.out.print(" "+ result[i][j]);
		  }
		  System.out.println("");
		}
	}
}

Output

Enter First Matrix (3 × 3) values:
2 3 4
6 2 7
7 8 4
Enter Second Matrix (3 × 3) values:
4 5 3
8 2 6
3 2 4
Addition two matrices is:
 6 8 7
 14 4 13
 10 10 8

Example 3: Matrix multiplication of two matrices (2 × 3 & 3 × 2 ), Resultant matrix is (2 × 2)

import java.util.Scanner; 
public class multimatrix {
	public static void main (String args[])
	{
		int[][] mat1,mat2,result; // No memory allocation
		int i,j,k,num, temp;
		mat1 = new int [2][3]; // memory is allocated to array
		mat2 = new int [3][2]; 
		result = new int [2][2]; 
		Scanner sc = new Scanner(System.in); 
		// Taking values from user
		System.out.println("Enter First Matrix (2 × 3) values: ");
		for(i=0;i<2;i++)
		{
			for (j=0;j<3;j++)
			{
				num = sc.nextInt();
				mat1[i][j] = num;	
			}
			
		}
		System.out.println("Enter Second Matrix (3 × 2) values: ");
		for(i=0;i<3;i++)
		{
			for (j=0;j<2;j++)
			{
				num = sc.nextInt();
				mat2[i][j] = num;	
			}
			
		}
		// Matrix Multiplication
		temp = 0;
		for(i=0;i<2;i++)
		{
			for (j=0;j<2;j++)
			{
				for(k=0;k<3;k++)
				{
					temp = temp + mat1[i][k]* mat2[k][j];	
					result[i][j] = temp;
				}
				temp =0;
			}
			   
		}
		// Displaying multiplication of two matrices 
		System.out.println("Result Matrix (2 × 2)is:\n");
		for (i=0;i<2;i++)
		{
		  for(j=0;j<2;j++)	
		  {
			System.out.print(" "+ result[i][j]);
		  }
		  System.out.println("");
		}
	}
}

Output

Enter First Matrix (2 × 3) values:
1 1 1
1 1 1
Enter Second Matrix (3 × 2) values:
1 1
1 1
1 1
Result Matrix (2 × 2)is:
 3 3
 3 3


© Copyright 2016-2024 by javatutelearn.com. All Rights Reserved.