write java code to print second max number in the array
Answer Posted / raja ram
public class SecondMaximumNumber{
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. number is
"+secondMaxNumber);
}
}
| Is This Answer Correct ? | 7 Yes | 5 No |
Post New Answer View All Answers
Can we create object of static class?
Which package is imported by default?
Explain the difference between extends thread vs implements runnable in java?
What is the difference between error and an exception?
What are runtime exceptions?
What is the purpose of the strictfp keyword?
What is predicate in java?
What is string array?
Why we cannot override static method?
What is meant by polymorphism?
What is java and its types?
What is the difference between compiler and jvm?
Does variable declaration allocate memory?
What is the purpose of sizeof operator?
Give few difference between constructor and method?