write java code to print second max number in the array
Answer Posted / rajaram
public static void main(String[] args){
int maxNumber = 0;
int secondMaxNumber = 0;
if(args.length == 0){
System.err.println("Number array is empty");
return;
}
for(int i=0; i < args.length; i++){
int currNumber = Integer.parseInt(args[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 ? | 28 Yes | 8 No |
Post New Answer View All Answers
What is import java util arraylist?
What is the difference between Error, defect,fault, failure and mistake?
take an array with -ve and +ve value both.find out the nearest value of 0(zero).if two values are same like(-2 and +2)then extract +2 is nearest of 0(zero).
Given a singly linked list, find the middle of the list in a single traversal without using temporary variable.
Can we have try block without catch block?
Can we create an object of static class in java?
What are the procedures?
Are generics important java?
Can we use static class instead of singleton?
What is difference between == and === in js?
What is an accessor?
Why main function is static?
Given a singly linked list, determine whether it contains a loop or not without using temporary space?
Is integer passed by reference in java?
What is final keyword in java?