Showing posts with label Find two maximum numbers in array. Show all posts
Showing posts with label Find two maximum numbers in array. Show all posts

Tuesday, 29 May 2018

Find two maximum numbers in array



Sample Program:-


public class MaxtwoNumbers {  
      public static void main(String[] args) {  
           int a[] = { 6, 99, 45, 3, 77 };  
           int max1 = 0;  
           int max2 = 0;  
           for (int i = 0; i < a.length; i++) {  
                if (a[i] > max1) {  
                     max2 = max1;  
                     max1 = a[i];  
                } else if (a[i] > max2) {  
                     max2 = a[i];  
                }  
           }  
           System.out.println("Maximum 2 numbers are:" + max1 + " and " + max2);  
      }  
 }  

Output:-


Maximum 2 numbers are:99 and 77  

Enjoy Coding.