Java Array Example For Interview Questions And Answers
The following Java program print the lowest element in upper triangular matrix see in the figure with colour. We use two dimensional array to store the numbers and find the lowest element in upper triangular matrix with the help of loop and conditional statements (if else) as shown below.
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
According to the above matrix the lowest element is 1.
Java array lowest element in upper triangular matrix
class LowestElementInUpperHalf{ public static void main(String... z) { int b=sum(new int[][] {{100,2,3},{0,50,6},{0,8,1}}); System.out.println("Lowest number in this array="+b); } static int sum(int d[][]) { int c=d[0][0]; for(int i=0;i<d.length;i++){ for(int j=(i+1)-1;j<d[i].length;j++){ if(j==i-1){ break; } if(c>d[i][j]) { c=d[i][j]; } } } return c; } }
Output
Lowest number in this array=1