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

Questions and Answers related to 2D array

The document contains a series of Java programs that demonstrate various operations on 2D arrays, including displaying elements, calculating diagonal sums, creating identity matrices, transposing matrices, and calculating row and column sums. Each program prompts the user for input dimensions and elements, processes the data, and outputs the results. The examples illustrate fundamental concepts in array manipulation and user input handling in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Questions and Answers related to 2D array

The document contains a series of Java programs that demonstrate various operations on 2D arrays, including displaying elements, calculating diagonal sums, creating identity matrices, transposing matrices, and calculating row and column sums. Each program prompts the user for input dimensions and elements, processes the data, and outputs the results. The examples illustrate fundamental concepts in array manipulation and user input handling in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Questions and Answers related to 2D array

Q. Write a Java program that takes a 2D array as input from the user and displays all its
elements.

import java.util.*;

class Q1
{
​ public static void main(String[] args)
{
​ ​ Scanner sc = new Scanner(System.in);
​ ​ System.out.println("Enter the row size of array: ");
​ ​ int row = sc.nextInt();
​ ​ System.out.println("Enter the col size of array: ");
​ ​ int col = sc.nextInt();
​ ​ int[][] arr = new int[row][col];
​ ​ System.out.println("Enter the "+(row*col)+" array elements: ");
​ ​ for(int i = 0;i<row;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<col;j++)
​ ​ ​ {
​ ​ ​ arr[i][j] = sc.nextInt();
​ ​ ​ }
​ ​ }
​ ​ System.out.println("Array elements are: ");
​ ​ for(int i = 0;i<row;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<col;j++)
​ ​ ​ {
​ ​ ​ System.out.print(arr[i][j]+" ");
​ ​ ​ }
​ ​ ​ System.out.println();
​ ​ }
​ }
}

Q. Write a Java program that takes a square matrix as input from the user, prints all the
elements of the left diagonal, and calculates the sum of both diagonals.

import java.util.*;

class Q2
{
​ public static void main(String[] args)
{
​ ​ Scanner sc = new Scanner(System.in);
​ ​ System.out.println("Enter the row size of array: ");
​ ​ int row = sc.nextInt();
​ ​ System.out.println("Enter the col size of array: ");
​ ​ int col = sc.nextInt();
​ ​ int[][] arr = new int[row][col];
​ ​ System.out.println("Enter the "+(row*col)+" array elements: ");
​ ​ for(int i = 0;i<row;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<col;j++)
​ ​ ​ {
​ ​ ​ arr[i][j] = sc.nextInt();
​ ​ ​ }
​ ​ }
​ ​ System.out.println("Array elements are: ");
​ ​ for(int i = 0;i<row;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<col;j++)
​ ​ ​ {
​ ​ ​ System.out.print(arr[i][j]+" ");
​ ​ ​ }
​ ​ ​ System.out.println();
​ ​ }
​ ​ int sum = 0;
​ ​ System.out.println("Left diagonal elements are: ");
​ ​ for(int i = 0;i<row;i++)
​ ​ {
​ ​ System.out.print(arr[i][i]+" ");
​ ​ sum = sum + arr[i][i];
​ ​ }
​ ​ System.out.println("\nSum = "+ sum);
​ }
}

Q. Write a Java program that takes a square matrix as input from the user, prints all the
elements of the right diagonal, and calculates the sum of both diagonals.

import java.util.*;

class Q3
{
​ public static void main(String[] args)
{
​ ​ Scanner sc = new Scanner(System.in);
​ ​ System.out.println("Enter the row size of array: ");
​ ​ int row = sc.nextInt();
​ ​ System.out.println("Enter the col size of array: ");
​ ​ int col = sc.nextInt();
​ ​ int[][] arr = new int[row][col];
​ ​ System.out.println("Enter the "+(row*col)+" array elements: ");
​ ​ for(int i = 0;i<row;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<col;j++)
​ ​ ​ {
​ ​ ​ arr[i][j] = sc.nextInt();
​ ​ ​ }
​ ​ }
​ ​ System.out.println("Array elements are: ");
​ ​ for(int i = 0;i<row;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<col;j++)
​ ​ ​ {
​ ​ ​ System.out.print(arr[i][j]+" ");
​ ​ ​ }
​ ​ ​ System.out.println();
​ ​ }
​ ​ int sum = 0;
​ ​ System.out.println("Right diagonal elements are: ");
​ ​ for(int i = 0;i<row;i++)
​ ​ {
​ ​ System.out.print(arr[i][row-1-i]+" ");
​ ​ sum = sum + arr[i][row-1-i];
​ ​ }
​ ​ System.out.println("\nSum = "+ sum);
​ }
}

Q. Write a Java program that creates an identity matrix with size nxn input from the user,
prints all the elements.

import java.util.*;

class Q4
{
​ public static void main(String[] args)
{
​ ​ Scanner sc = new Scanner(System.in);
​ ​ System.out.println("Enter the size of n: ");
​ ​ int n = sc.nextInt();
​ ​ int[][] arr = new int[n][n];
​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<n;j++)
​ ​ ​ {
​ ​ ​ if(i==j)
​ ​ ​ ​ arr[i][j] = 1;
​ ​ ​ else
​ ​ ​ ​ arr[i][j] = 0;
​ ​ ​ }
​ ​ }
​ ​ System.out.println("Identity matrix is: ");
​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<n;j++)
​ ​ ​ {
​ ​ ​ System.out.print(arr[i][j]+" ");
​ ​ ​ }
​ ​ ​ System.out.println();
​ ​ }
​ }
}

Q. Write a Java program that creates a matrix with size nxm input from the user, prints a
transpose matrix.

import java.util.*;

class Q5
{
​ public static void main(String[] args)
{
​ ​ Scanner sc = new Scanner(System.in);
​ ​ System.out.println("Enter the row size of matrix: ");
​ ​ int n = sc.nextInt();
​ ​ System.out.println("Enter the col size of matrix: ");
​ ​ int m = sc.nextInt();
​ ​ int[][] arr = new int[n][m];
​ ​ System.out.println("Enter the "+(n*m)+" matrix elements: ");
​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ for(int j = 0;j<m;j++)
​ ​ {
​ ​ ​ arr[i][j] = sc.nextInt();
​ ​ }
​ ​ }

​ ​ System.out.println("Original matrix is: ");


​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<m;j++)
​ ​ ​ {
​ ​ ​ System.out.print(arr[i][j]+" ");
​ ​ ​ }
​ ​ ​ System.out.println();
​ ​ }
​ ​ System.out.println("Transpose matrix is: ");
​ ​ for(int i = 0;i<m;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<n;j++)
​ ​ ​ {
​ ​ ​ System.out.print(arr[j][i]+" ");
​ ​ ​ }
​ ​ ​ System.out.println();
​ ​ }
​ }
}

Or you can consider an additional 2d array to solve the above problem. Here the another
solution

import java.util.*;

class Q5
{
​ public static void main(String[] args)
{
​ ​ Scanner sc = new Scanner(System.in);
​ ​ System.out.println("Enter the row size of matrix: ");
​ ​ int n = sc.nextInt();
​ ​ System.out.println("Enter the col size of matrix: ");
​ ​ int m = sc.nextInt();
​ ​ int[][] arr = new int[n][m];
​ ​ System.out.println("Enter the "+(n*m)+" matrix elements: ");
​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ for(int j = 0;j<m;j++)
​ ​ {
​ ​ ​ arr[i][j] = sc.nextInt();
​ ​ }
​ ​ }
​ ​ System.out.println("Original matrix is: ");
​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<m;j++)
​ ​ ​ {
​ ​ ​ System.out.print(arr[i][j]+" ");
​ ​ ​ brr[j][i] = arr[i][j];
​ ​ ​ }
​ ​ ​ System.out.println();
​ ​ }

​ ​ System.out.println("Transpose matrix is: ");
​ ​ for(int i = 0;i<m;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<n;j++)
​ ​ ​ {
​​ ​ ​ System.out.print(brr[i][j]+" ");
​ ​ ​ }
​ ​ ​ System.out.println();
​ ​ }
}
}

Q. Write a Java program that creates a matrix with size nxm input from the user, prints
row wise and column wise sum.

import java.util.*;

class Q6
{
​ public static void main(String[] args)
{
​ ​ Scanner sc = new Scanner(System.in);
​ ​ System.out.println("Enter the row size of matrix: ");
​ ​ int n = sc.nextInt();
​ ​ System.out.println("Enter the col size of matrix: ");
​ ​ int m = sc.nextInt();
​ ​ int[][] arr = new int[n][m];
​ ​ System.out.println("Enter the "+(n*m)+" matrix elements: ");
​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ for(int j = 0;j<m;j++)
​ ​ {
​ ​ ​ arr[i][j] = sc.nextInt();
​ ​ }
​ ​ }
​ ​ System.out.println("Original matrix is: ");
​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ ​ for(int j = 0;j<m;j++)
​ ​ ​ {
​ ​ ​ System.out.print(arr[i][j]+" ");
​ ​ ​ }
​ ​ System.out.println();
​ ​ }

​ ​ System.out.println("Row-wise matrix sum: ");
​ ​ for(int i = 0;i<n;i++)
​ ​ {
​ ​ ​ int sum = 0;
​ ​ ​ for(int j = 0;j<m;j++)
​ ​ ​ {
​ ​ ​ sum = sum + arr[i][j];
​ ​ ​ }
​ ​ ​ System.out.println((i+1)+" row sum = "+ sum);
​ ​ }

​ ​ System.out.println("Column-wise matrix sum: ");
​ ​ for(int i = 0;i<m;i++)
​ ​ {
​ ​ ​ int sum = 0;
​ ​ ​ for(int j = 0;j<n;j++)
​ ​ ​ {
​ ​ ​ sum = sum + arr[j][i];
​ ​ ​ }
​ ​ ​ System.out.println((i+1)+" column sum = "+ sum);
​ ​ }
​ }
}

You might also like