0% found this document useful (0 votes)
5 views

Computer applications class 10

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Computer applications class 10

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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);


}
}

You might also like