Write a java program to find the largest number in an array

Write a java program to find the largest number in an array. Find the largest two numbers in an array with a Java program. Enter the array’s size, followed by all of the array’s elements. Using double for loops, we first sort the array in decreasing order, yielding the first two elements as output. The Java Program to Find the Largest Two Numbers in an Array source code is available here.

import java.util.Scanner;
public class Main
{
public static void main (String[] args)
{
Scanner scn = new Scanner (System.in);
System.out.print("Enter no f elements you want in array");
int n = scn.nextInt();

int array[] = new int[n];
System.out.println("Enter all the elements");
for (int i = 0; i < array.length; i++)
{
array[i] = scn.nextInt();
}

int high1, high2, temp;

high1 = array[0];
high2 = array[1];

if (high1 < high2)
{
temp = high1;
high1 = high2;
high2 = temp;
}

for (int i = 2; i < array.length; i++)
{
if (array[i] > high1)
{
high2 = high1;
high1 = array[i];
}
else if (array[i] > high2 && array[i] != high1)
{
high2 = array[i];
}
}

System.out.println ("The highest number"+high1);
System.out.println ("The lowest number"+ high2);

}
}

 

Output:

Enter no f elements you want in array2
Enter all the elements
45
565
The highest number565
The lowest number45