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.
In this programs, we can see step by step procedure for completion of the program.
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
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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
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.