Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Write Java programs for following: 1. To create a 2D array to store numbers.

ID: 3910264 • Letter: 3

Question

3. Write Java programs for following: 1. To create a 2D array to store numbers. Take the input of the array. Display the contents of the array in the matrix 2. To create two 2D arrays and carry out the following operations: sum of the two matrices and difference of the two 3. To create a matrix of size of MXN, find the sum of each row, sum of each column and if it is a square matrix then find . To create a square matrix of order 3x3 containing integers, using a method CREATE). The program should then use a form. Find and display the sum of all elements, sum of all even numbers, and sum of all odd numbers matrices. Display the matrices in a proper matrix format using a method the sum of diagonal elements also. M and N are user input. function SWAP() which swaps two integer values, to find the transpose of the matrix, and display the transpose using a function DISPLAY()

Explanation / Answer

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// declaring variables

int m, n;

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of rows: ");

m = sc.nextInt();

System.out.print("Enter the number of columns: ");

n = sc.nextInt();

// declaring variables

int[][] a = new int[m][n];

// for each row

for(int row=0; row<m; row++)

{

System.out.printf("Enter elements in row%d: ",row+1);

// for every number in row, taking input

for(int col=0; col<n; col++)

{

a[row][col] = sc.nextInt();

}

}

int total = 0, evenSum = 0, oddSum = 0;

// printing output

System.out.println(" Matrix is given below");

for(int row=0; row<m; row++)

{

for(int col=0; col<n; col++)

{

System.out.print(a[row][col] + " ");

total += a[row][col];

if(a[row][col]%2 == 0)

evenSum += a[row][col];

else

oddSum += a[row][col];

}

System.out.println();

}

System.out.println("Total is "+ total);

System.out.println("Even sum is "+ evenSum);

System.out.println("Odd sum is "+ oddSum);

}

}

/*SAMPLE output

Enter the number of rows: 3

Enter the number of columns: 4

Enter elements in row1: 3 6 5 7

Enter elements in row2: 8 7 6 3

Enter elements in row3: 2 4 3 9

Matrix is given below

3 6 5 7

8 7 6 3

2 4 3 9

Total is 63

Even sum is 26

Odd sum is 37

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote