Java two dimensional array program

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn java two dimensional array program.

java two dimensional array program

Two dimensional array java program is made up of rows and columns. To print two dimensional array we need two “for loops”. First “for loop” represent row and the second “for loop” represent column.

Also read – arrays in java

Here’s the java program where user is asked to enter row size and column size. Then array elements is entered. Finally two dimensional array is displayed.

import java.util.Scanner;

public class TwoDimensionalArray
{
public static void main(String[] args)
{
int row, column, a, b;
int number[][] = new int[5][5];
Scanner sc = new Scanner(System.in);

System.out.print("Enter no. of rows (max 5) : ");
row = sc.nextInt();
System.out.print("Enter no. of column (max 5) : ");
column = sc.nextInt();

System.out.print("Enter " + (row * column) + " numbers : ");
for(a = 0; a < row; a++)
{
for(b = 0; b < column; b++)
{
number[a][b] = sc.nextInt();
}
}
System.out.print("Two dimensional array :\n");
for(a = 0; a < row; a++)
{
for(b = 0; b < column; b++)
{
System.out.print(number[a][b]+ " ");
}
System.out.println();
}
sc.close();
}
}

Output:

Enter no. of rows (max 5) : 2
Enter no. of column (max 5) : 2
Enter 4 numbers : 2
4
6
8
Two dimensional array :
2 4
6 8

Related Posts