Sponsered Links
Categories
Sponsered Links

Find Second Largest Number

 

Through the given code, we are finding the second largest number from the array of numbers.

public class SecondLargestNumber{
public static void main(String[] args) {
int secondlargest= -1;
int largest = -1;
int num[] ={5,7,6,1,9,20};
for (int i=0;i < num.length;i++){
if(largest < num[i] ){
secondlargest = largest;
largest = num[i];
}
if(secondlargest < num[i] && largest != num[i] )
secondlargest = num[i];
}
System.out.println("Second Largest number is: "+secondlargest);
}
}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved