JAVA SP T


Lets dive into snapshot of my programs!💻

program:1.Enter the Matrix of rows and columns entered by the user and print in matrix format

💡Code:

import java.util.Scanner;
              class MatrixInputAndPrint {
                  public static void main(String[] args) {
                      Scanner scanner = new Scanner(System.in);
              
                      System.out.print("Enter the number of rows: ");
                      int rows = scanner.nextInt();
              
                      System.out.print("Enter the number of columns: ");
                      int columns = scanner.nextInt();
              
                      int[][] matrix = new int[rows][columns];
              
                      System.out.println("Enter the matrix elements:");
                      for (int i = 0; i < rows; i++) {
                          for (int j = 0; j < columns; j++) {
                              System.out.print("Enter element at position [" + i + "][" + j + "]: ");
                              matrix[i][j] = scanner.nextInt();
                          }
                      }
              
                      System.out.println("Matrix:");
              
                      for (int i = 0; i < rows; i++) {
                          for (int j = 0; j < columns; j++) {
                              System.out.print(matrix[i][j] + "\t");
                          }
                          System.out.println(); 
                      }
                  }
              }
              

📸Output :

output