Assignment: Module 03- Part 03
![]() |
Assignment: Module 03- Part 03 |
Write a class TwoArray to accept the number of rows and columns and array values, and display it all using control structures and array objects.
Input:
Enter the no of rows and cols
2
3
Enter the no. 33
Enter the no. 55
Enter the no. 22
Enter the no 33
Enter the no 44
Enter the no 55
Output:
The given matrix
33, 55, 22
33, 44, 55
import java.util.Scanner;
public class TwoArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.println("Enter the number of columns: ");
int cols = sc.nextInt();
int[][] array = new int[rows][cols];
System.out.println("Enter the array values: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = sc.nextInt();
}
}
System.out.println("The given matrix: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
And here is the output for the given input:
Enter the number of rows:
2
Enter the number of columns:
3
Enter the array values:
33
55
22
33
44
55
The given matrix:
33 55 22
33 44 55