-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5.java
More file actions
35 lines (29 loc) · 1.03 KB
/
Q5.java
File metadata and controls
35 lines (29 loc) · 1.03 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
import java.util.*;
import java.io.*;
public class Q5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int n = scanner.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
int largest = array[0];
int secondLargest = array[0];
for (int i = 0; i < n; i++) {
if (array[i] > largest) {
secondLargest = largest;
largest = array[i];
} else if (array[i] > secondLargest && array[i] != largest) {
secondLargest = array[i];
}
}
if (secondLargest == array[0]) {
System.out.println("There is no second largest element in the array.");
} else {
System.out.println("The second largest element in the array is: " + secondLargest);
}
}
}