Answer to Question #322568 in Java | JSP | JSF for Nhm

Question #322568

Write a Java program to do the following

a) Read two-dimensional array (3 x3)

b) Find the diagonal elements of the array 


1
Expert's answer
2022-04-02T09:58:57-0400
import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int[][] arr = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};

        for(int i = 0; i < 3; i++)
            System.out.print(arr[i][i] + " ");

        System.out.println("");
        int j = 0;

        for(int i = 2; i >= 0; i--){
            System.out.print(arr[j++][i] + " ");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS