Below example show how to find first and second largest Number From int type Array.
public class TwoMaximumNumberFromList {
public static void method(int[] num){
int firstMax = 0;
int secondMax = 0;
for(int n : num){
if(n > firstMax){
secondMax = firstMax;
firstMax = n;
}
else if(secondMax < n){
secondMax = n;
}
}
System.out.println("FIRST MAXIMUM NUMBER : "+firstMax);
System.out.println("SECOND MAXIMUM NUMBER : "+secondMax);
}
public static void main(String[] args) {
int[] num={5,6,9,3};
TwoMaximumNumberFromList.method(num);
}
}
Program Output :-
FIRST MAXIMUM NUMBER : 9SECOND MAXIMUM NUMBER : 6

No comments:
Post a Comment