Find two maximum numbers in array in one iteration in java


In this core java programming tutorial we will write a program to Find two maximum numbers in array in one iteration in java.



Find two maximum numbers in array in one iteration.

Full Program/SourceCode/ example to Find two maximum numbers in array in one iteration in java >
/** Copyright (c), AnkitMittal www.JavaMadeSoEasy.com */
public class FindTwoMaximumNumbersInArrayExample {
   public static void main(String a[]) {
          int ar[] = { 66, 7, 9, 70, 89, 11, 2, 93 };
          int maximum1 = 0;
          int Maximum2 = 0;
          for (int i = 0; i < ar.length; i++) {  //only one iteration
                 if (maximum1 < ar[i]) {
                       Maximum2 = maximum1;
                       maximum1 = ar[i];
                 } else if (Maximum2 < ar[i]) {
                       Maximum2 = ar[i];
                 }
          }
          System.out.println("Maximum1 : " + maximum1);
          System.out.println("Maximum2 : " + Maximum2);
   }
}
/*OUTPUT
Maximum1 : 93
Maximum2 : 89
*/




So in this core java programming tutorial we wrote a program on how to Find two maximum numbers in array in one iteration in java.


Previous program                                                                  Next program


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>





No comments:

Post a Comment