Java Program to Find the Largest Two Numbers in an Array

This is a Java Program to Find the Largest Two Numbers in a Given Array.

Enter size of array and then enter all the elements of that array. Now we first sort the array in decreasing order using double for loops and hence get the first two elements as output.

Here is the source code of the Java Program to Find the Largest Two Numbers in a Given Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class largest_and_second
  3. {
  4. 	public static void main (String[] args)
  5. 	{
  6. 		Scanner scn = new Scanner (System.in);
  7. 		System.out.print("Enter no. of elements you want in array:");
  8.                 int n = scn.nextInt();
  9.  
  10. 		int array[] = new int[n];
  11.                 System.out.println("Enter all the elements:");
  12. 		for (int i = 0; i < array.length; i++)
  13. 		{
  14. 			array[i] = scn.nextInt();
  15. 		}
  16.  
  17. 		int largest1, largest2, temp;
  18.  
  19. 		largest1 = array[0];
  20. 		largest2 = array[1];
  21.  
  22. 		if (largest1 < largest2)
  23. 		{
  24. 			temp = largest1;
  25. 			largest1 = largest2;
  26. 			largest2 = temp;
  27. 		}
  28.  
  29. 		for (int i = 2; i < array.length; i++)
  30. 		{
  31. 			if (array[i] > largest1)
  32. 			{
  33. 				largest2 = largest1;
  34. 				largest1 = array[i];
  35. 			}
  36. 			else if (array[i] > largest2 && array[i] != largest1)
  37. 			{
  38. 				largest2 = array[i];
  39. 			}
  40. 		}
  41.  
  42. 		System.out.println ("The First largest is " + largest1);
  43. 		System.out.println ("The Second largest is " + largest2);
  44.  
  45. 	}
  46. }

Output:

Enter no. of elements you want in array:5
Enter all the elements:
1
5
4
8
7
The First largest is 8
The Second largest is 7
 
Enter no. of elements you want in array:7
Enter all the elements:
3
2
1
3
2
1
3
The First largest is 3
The Second largest is 2

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.