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
Can we pass null as argument in java?
Is java written in c?
v-model life cycle
Is void a return type?
What is a finally block? Is there a case when finally will not execute?
what is server side caching?
What is the difference between form & report?
What is the null?
how to open and edit XML file in Weblogic???
What is a flag variable?
How many wrapper classes are there in java?
What is token in java?
What are the approaches that you will follow for making a program very efficient?
Is java hard to learn?
What methods are used to get and set the text label displayed by a button object?