-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargerElement.java
More file actions
55 lines (49 loc) · 1.64 KB
/
Copy pathLargerElement.java
File metadata and controls
55 lines (49 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
public class LargerElement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows : ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns : ");
int columns = sc.nextInt();
int array [][] = new int[rows][columns];
for(int i =0; i<rows; i++){
for(int j =0; j<columns;j++){
array[i][j] = sc.nextInt();
}
}
System.out.println("Enter \n1.Largest Element \n2.Smallest Element");
int choice = sc.nextInt();
if(choice==1){
System.out.println("The largest element in the array is " + largestElement(array, rows, columns));
}
else if(choice==2){
System.out.println("The smallest element in the array is " + smallestElement(array, rows, columns));
}
else{
System.out.println("Invalid choice ");
}
}
public static int largestElement(int [][]arr, int rows,int columns){
int larg = arr[0][0];
for(int i=0; i<rows; i++){
for(int j =0; j<columns ;j++){
if(arr[i][j]>larg){
larg = arr[i][j];
}
}
}
return larg;
}
public static int smallestElement(int arr [][], int rows , int columns ){
int small = arr[0][0];
for(int i =0; i<rows; i++){
for(int j =0; j<columns; j++){
if(arr[i][j]<small ){
small = arr[i][j];
}
}
}
return small;
}
}