write java code to print second max number in the array
Answer Posted / sujeev
Ranjaram is correct.I modified and testes it.
public class test123 {
public static void main(String[] args){
int maxNumber = 0;
int secondMaxNumber = 0;
int[] anArray;
anArray =new int [10];
anArray[0] = 100;
anArray[1] = 200;
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
if(anArray.length == 0){
System.err.println("Number array is empty");
return;
}
for(int i=0; i < anArray.length; i++){
int currNumber = anArray[i];
if(maxNumber < currNumber){
secondMaxNumber = maxNumber;
maxNumber = currNumber;
}else if(secondMaxNumber < currNumber){
secondMaxNumber = currNumber;
}
}
System.err.println("Max. number is "+maxNumber);
System.err.println("Second Max.
is "+secondMaxNumber);
}
}
Is This Answer Correct ? | 2 Yes | 3 No |
Post New Answer View All Answers
Why heap memory is called heap?
How many functional interfaces does java 8 have?
Does java allow overriding static methods ?
How do you calculate roots in java?
What does indexof return in java?
What about main() method in java ?
What does sizeof return?
Which methods are used during serialization and deserialization process?
How will you add panel to a frame?
What is super constructor?
What is the symbol for average?
What is math floor in java?
What is the static method?
Which are different kinds of source code?
What is the use of generics? When was it added to the Java development Kit?