import java.util.
Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns: ");
int cols = sc.nextInt();
int[][] array = new int[rows][cols];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = sc.nextInt();
}
}
System.out.println("First column elements are:");
for (int i = 0; i < rows; i++)
{
System.out.println(array[i][0]);
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns: ");
int cols = sc.nextInt();
int[][] array = new int[rows][cols];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array[i][j] = sc.nextInt();
}
}
System.out.println("Top row elements are:");
for (int j = 0; j < cols; j++) {
System.out.println(array[0][j]);
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns: ");
int cols = sc.nextInt();
int[][] array = new int[rows][cols];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = sc.nextInt();
}
}
// Print the last column
System.out.println("Last column elements are:");
for (int i = 0; i < rows; i++) {
System.out.println(array[i][cols - 1]); // Access the last element of each row
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns: ");
int cols = sc.nextInt();
int[][] array = new int[rows][cols];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = sc.nextInt();
}
}
System.out.println("Last column elements are:");
for (int i = 0; i < rows; i++) {
System.out.println(array[i][cols - 1]);
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix (n x n): ");
int n = sc.nextInt();
int[][] array = new int[n][n];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
array[i][j] = sc.nextInt();
}
}
System.out.println("Left diagonal elements are:");
for (int i = 0; i < n; i++) {
System.out.println(array[i][i]);
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix (n x n): ");
int n = sc.nextInt();
int[][] array = new int[n][n];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
array[i][j] = sc.nextInt();
}
}
System.out.println("Right diagonal elements are:");
for (int i = 0; i < n; i++) {
System.out.println(array[i][n - i - 1]); // Access elements on the right diagonal
}
}
}
Q.Fill a 2D array of order 4 x 4 print the left and right diagonal elements of the
array.
import java.util.Scanner;
public class DiagonalElements
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int[][] array = new int[4][4];
System.out.println("Enter the elements for a 4x4 array:");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
System.out.print("Enter element at position [" + i + "][" + j + "]: ");
array[i][j] = scanner.nextInt();
}
}
Q.Fill a 2D array of order 4 x 4 print the left and right diagonal elements of the
array.
import java.util.Scanner;
public class DiagonalElements
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int[][] array = new int[4][4];
System.out.println("Enter the elements for a 4x4 array:");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
System.out.print("Enter element at position [" + i + "][" + j + "]: ");
array[i][j] = scanner.nextInt();
}
}
Fill a double dimensional array of order 4 X 4 print the largest element of the left
diagonal.
import java.util.Scanner;
public class LargestLeftDiagonal
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
double[][] array = new double[4][4];
System.out.println("Enter the elements for a 4x4 array:");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
System.out.print("Enter element at position [" + i + "][" + j + "]: ");
array[i][j] = scanner.nextDouble();
}
}
double largest = array[0][0];
for (int i = 1; i < 4; i++)
{
if (array[i][i] > largest)
{
largest = array[i][i];
}
}
System.out.println("\nLargest element in the Left Diagonal: " + largest);
}
}