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
What is static variable with example?
What is the difference between an interface and an abstract class?
What are strings in physics?
What is java util collection?
What are constructors in java?
What are the 3 types of control structures?
Is null function in java?
What are the java ide’s?
How we can generate random numbers in java?
What is thread count in java?
Give differences between Quicksort &Mergesort. When should these sorts be used andwhat is their running time in java?
What is console based application in java?
What is the use of beaninfo?
What are operators and its types?
What about interthread communication and how it takes place in java?