Find elements

Java code to largest and second largest elements in array

Java code to largest and second largest elements in array

In this tutorial, we will discuss the  concept of Java code to  largest and second largest elements in array.

In this topic, we will learn how to find the largest and second largest elements of array elements in Java programming language

This program takes “n” number of elements and Enter the elements of the array as input from the user. then, this program finds and displays the largest and second largest elements from the array using for loop and if statements.

largest and second largest element in array

In this programs, we can see step by step procedure for completion of the program.

  • Essential variable declaration
  • array declaration
  • Get input from the user for array length
  • Get input from the user for array elements
  • loop for find largest and second largest value
  • Display result on the screen

Find largest and second largest elements from integer Array

Program 1

import java.util.*;
class FandSnum{
public static void main (String args[]){
int size;//variable declaration for size of array
int firstNum=0, secondNum=0;
//Using scanner class
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of element: ");
size=scan.nextInt();//taking input from user for size of array
int arr[]=new int[size];//declaration of array


for(int i=0; i<size; i++){
    System.out.print("Enter the array element "+(i+1)+" :");
arr[i]=scan.nextInt();//taking input for array elements

if(firstNum<arr[i]){
secondNum=firstNum;
firstNum=arr[i];
}
else if(secondNum<arr[i]){
secondNum=arr[i];
}
}
System.out.println("\nThe largest number is: "+firstNum);
System.out.println("The second largest number is: "+secondNum);
}//display result on the screen
}

When the above code is executed, it produces the following results

Enter the number of elements: 6
Enter the array element 1: 34
Enter the array element 2: 76
Enter the array element 3: 21
Enter the array element 4: 56
Enter the array element 5: 89
Enter the array element 6: 54

The largest number is: 89
The second largest number is: 76

 

Find largest and second largest elements from float Array

Program 2

import java.util.*;
class FandSnumfloat{
public static void main (String args[]){
int size;//variable declaration for size of an array
float firstNum=0, secondNum=0;
//Using scanner class
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of element: ");
size=scan.nextInt();//taking input from user for size of array
float arr[]=new float[size];//declaration of array


for(int i=0; i<size; i++){
    System.out.print("Enter the array element "+(i+1)+" :");
arr[i]=scan.nextFloat();//Takes input for array elements

if(firstNum<arr[i]){
secondNum=firstNum;
firstNum=arr[i];
}
else if(secondNum<arr[i]){
secondNum=arr[i];
}
}
System.out.println("\nlargest number is "+firstNum);
System.out.println("Second largest number is "+secondNum);
}//display result on the screen
}

When the above code is executed, it produces the following results

Enter the number of the elements: 6
Enter the array elements 1: 34.67
Enter the array elements 1: 76.43
Enter the array elements 1: 56.7
Enter the array elements 1: 54.2
Enter the array elements 1: 35.9
Enter the array elements 1: 98.65

First largest number is  98.65

Second largest number is  76.43

 

Suggested for you

Single dimension array in Java

Operator in Java

For loop in Java

If statements of java

Python program to middle among three numbers
Cpp program to largest and second largest elements in array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • The program number 1 , not giving output properly if you enter duplicates :
    Output:
    Enter the number of element: 10
    Enter the array element 1 :1
    Enter the array element 2 :1
    Enter the array element 3 :1
    Enter the array element 4 :13
    Enter the array element 5 :3
    Enter the array element 6 :3
    Enter the array element 7 :15
    Enter the array element 8 :15
    Enter the array element 9 :16
    Enter the array element 10 :16

    The largest number is: 16
    The second largest number is: 16

    Check twice before posting anything because each and every program will decide the fate of the student in the interview room.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago