Write a Java  Program to find largest and second largest in an array

Write a Java Program to find largest and second largest in an array

Write a Java Program to find largest and second largest in an array

Program
public class Main 
{
    public static void main(String[] args) 
    {
        int nums[] = { 5, 34, 78, 2, 45, 1, 99, 23 }; 
        int maxOne = 0;
        int maxTwo = 0; 
        for (int i=0;i<nums.length; i++) 
        {
            if (maxOne < nums[i])
            {
                maxTwo = maxOne;
                maxOne = nums[i];
            } 
            else if (maxTwo < nums[i])
            {
                maxTwo = nums[i]; 
            }
        } 
        System.out.println("Largest Number: " + maxOne); 
        System.out.println("Second Largest Number: " + maxTwo);
    }
}


Output:

Largest Number: 99
Second Largest Number: 78



More Questions


40 . Write a Program to Find largest and smallest number in an array in java
41 . Write a Java Program to find largest and second largest in an array
42 . Write a java program to find the index of the largest number in an array
43 . Write a java program to Find the index of the smallest number in an array
44 . Write a Program to remove duplicate element in an array
45 . Write a java Program to print odd and even numbers from an array
46 . Write a java program to add two matrix
47 . Write a java Program to check given matrix is null matrix
48 . Add all the digits of a given positive integer until the result has a single digit
49 . Write a Java program to accept a float value of number and return a rounded float value



For latest job updates join Telegram Channel: https://t.me/sateeshm

Apply For The Job

Apply For Job

Java Material

Python Material

PLSQL Material

.Net Material

Java Practice Programs

400 Java Programs