Categories
java matrix

create 2×2 matrix using java

In this post, we will learn to create 2×2 matrix using java

We all know that In mathematics Matrix is a two dimensional array with rows and column as like above image. Here we are going to create that matrix using java

Java have a feature to create two dimensional array as like below

Two dimensional array Syntax
			int a[][] = { { 1, 2 }, { 2, 4 } };

The above array will convert as like below image

Example
public class CreateMatrixExample {
	public static void main(String[] args) {
		try {

			// Creating 2x2 matrix
			int a[][] = { { 1, 2 }, { 2, 4 } };

			// printing above 2d array in matrix format
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < a[i].length; j++) {
					System.out.print(a[i][j] + " ");
				}
				System.out.println();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
Output
1 2 
2 4 
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/CreateMatrixExample.java

Leave a Reply

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