-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecond_LargestNumber.java
More file actions
26 lines (25 loc) · 924 Bytes
/
Second_LargestNumber.java
File metadata and controls
26 lines (25 loc) · 924 Bytes
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
// WAP to find the largest and Second largest Number
import java.util.Scanner;
public class Second_LargestNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the Array: ");
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int Max = 0;
int Second_max = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i]>Max) {
Second_max = Max; // The previous max becomes the new second_max
Max = arr[i];
} else if(arr[i] > Second_max && arr[i] < Max) {
Second_max = arr[i];
}
}
System.out.println("The Largest Num: "+Max);
System.out.println("The Second Largest Num: "+Second_max);
}
}