Saturday, 3 September 2016

Java Program to find First And Second Maximum Number From int type Array ?

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 : 9

SECOND MAXIMUM NUMBER : 6




No comments:

Post a Comment