0% found this document useful (0 votes)
27 views13 pages

Java 2D Array Diagonal Elements Guide

The document contains multiple Java programs that demonstrate how to create and manipulate 2D arrays. Each program prompts the user to input the dimensions and elements of the array, then performs specific tasks such as printing the first column, top row, last column, left diagonal, right diagonal, and finding the largest element in the left diagonal. The examples are designed for a 4x4 array and include both integer and double data types.

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views13 pages

Java 2D Array Diagonal Elements Guide

The document contains multiple Java programs that demonstrate how to create and manipulate 2D arrays. Each program prompts the user to input the dimensions and elements of the array, then performs specific tasks such as printing the first column, top row, last column, left diagonal, right diagonal, and finding the largest element in the left diagonal. The examples are designed for a 4x4 array and include both integer and double data types.

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

Scanner;

public class Main {


public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int rows = [Link]();
[Link]("Enter the number of columns: ");
int cols = [Link]();

int[][] array = new int[rows][cols];

[Link]("Enter the elements of the array:");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = [Link]();
}
}

[Link]("First column elements are:");


for (int i = 0; i < rows; i++)
{
[Link](array[i][0]);
}
}
}
import [Link];

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");


int rows = [Link]();
[Link]("Enter the number of columns: ");
int cols = [Link]();

int[][] array = new int[rows][cols];


[Link]("Enter the elements of the array:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array[i][j] = [Link]();
}
}

[Link]("Top row elements are:");


for (int j = 0; j < cols; j++) {
[Link](array[0][j]);
}
}
}
import [Link];

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");


int rows = [Link]();
[Link]("Enter the number of columns: ");
int cols = [Link]();

int[][] array = new int[rows][cols];


[Link]("Enter the elements of the array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = [Link]();
}
}

// Print the last column


[Link]("Last column elements are:");
for (int i = 0; i < rows; i++) {
[Link](array[i][cols - 1]); // Access the last element of each row
}
}
}
import [Link];

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");


int rows = [Link]();
[Link]("Enter the number of columns: ");
int cols = [Link]();

int[][] array = new int[rows][cols];


[Link]("Enter the elements of the array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = [Link]();
}
}

[Link]("Last column elements are:");


for (int i = 0; i < rows; i++) {
[Link](array[i][cols - 1]);
}
}
}
import [Link];

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the size of the square matrix (n x n): ");


int n = [Link]();

int[][] array = new int[n][n];


[Link]("Enter the elements of the matrix:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
array[i][j] = [Link]();
}
}

[Link]("Left diagonal elements are:");


for (int i = 0; i < n; i++) {
[Link](array[i][i]);
}
}
}
import [Link];

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the size of the square matrix (n x n): ");


int n = [Link]();

int[][] array = new int[n][n];


[Link]("Enter the elements of the matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
array[i][j] = [Link]();
}
}

[Link]("Right diagonal elements are:");


for (int i = 0; i < n; i++) {
[Link](array[i][n - i - 1]); // Access elements on the right diagonal
}
}
}
[Link] a 2D array of order 4 x 4 print the left and right diagonal elements of the
array.

import [Link];

public class DiagonalElements


{
public static void main(String[] args)
{

Scanner scanner = new Scanner([Link]);

int[][] array = new int[4][4];

[Link]("Enter the elements for a 4x4 array:");

for (int i = 0; i < 4; i++)


{
for (int j = 0; j < 4; j++)
{
[Link]("Enter element at position [" + i + "][" + j + "]: ");

array[i][j] = [Link]();
}
}
[Link] a 2D array of order 4 x 4 print the left and right diagonal elements of the
array.

import [Link];

public class DiagonalElements


{
public static void main(String[] args)
{

Scanner scanner = new Scanner([Link]);

int[][] array = new int[4][4];

[Link]("Enter the elements for a 4x4 array:");

for (int i = 0; i < 4; i++)


{
for (int j = 0; j < 4; j++)
{
[Link]("Enter element at position [" + i + "][" + j + "]: ");

array[i][j] = [Link]();
}
}
Fill a double dimensional array of order 4 X 4 print the largest element of the left
diagonal.

import [Link];

public class LargestLeftDiagonal


{
public static void main(String[] args)
{

Scanner scanner = new Scanner([Link]);

double[][] array = new double[4][4];

[Link]("Enter the elements for a 4x4 array:");

for (int i = 0; i < 4; i++)


{
for (int j = 0; j < 4; j++)
{
[Link]("Enter element at position [" + i + "][" + j + "]: ");
array[i][j] = [Link]();
}
}
double largest = array[0][0];

for (int i = 1; i < 4; i++)


{
if (array[i][i] > largest)
{
largest = array[i][i];
}
}

[Link]("\nLargest element in the Left Diagonal: " + largest);


}
}

You might also like