Write a Java program to do the following
a) Read two-dimensional array (3 x3)
b) Find the diagonal elements of the array
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] + " ");
}
}
}
Comments
Leave a comment