Codeforcoding

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.

Java code to largest and second largest element in array
largest and second largest element in array

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

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
Exit mobile version