• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Test Cases
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » How to Find Two Max Numbers From an Array In Java?

How to Find Two Max Numbers From an Array In Java?

Last Updated on: November 5, 2018 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • How to Find Two Max Numbers From an Array In Java?

How to Find Two Max Numbers From an Array In Java?

package com.softwaretestingblog.programs;
public class FindTwoMaxNumbers {
   public void findTwoMaxNumbers(int[] array){
      int maxOne = 0;
      int maxTwo = 0;
      for(int i:array)
      {
         if(maxOne < i)
         {
            maxTwo = maxOne;
            maxOne =i;
         }
         else if(maxTwo < i)
         {
            maxTwo = i;
         }
      }

      System.out.println("First Maximum Number: "+maxOne);
      System.out.println("Second Maximum Number: "+maxTwo);
   }
   public static void main(String a[]){
      int num[] = {4,23,67,1,76,1,98,13};
      FindTwoMaxNumbers obj = new FindTwoMaxNumbers();
      obj.findTwoMaxNumbers(num);
      //obj.findTwoMaxNumbers(new int[]{4,5,6,90,1});
   }
}
Check Also: Basic Array Programs

Output:

First Maximum Number: 98
Second Maximum Number: 76

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2022 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers