Answer:
One of the ways is to sort the array, then print the last element of the sorted array.
In Java, you can make use of the following code
import java.util.Scanner;
public class Assignment{
public static void main(String [] args)
{
int n;
Scanner input = new Scanner(System.in);
System.out.print("Array Length: ");
n = input.nextInt();
int [] myarray = new int[n];
System.out.print("Input Array: ");
for(int i=0;i<n;i++)
{
myarray[i]=input.nextInt();
}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (myarray[i] > myarray[j])
{
temp = myarray[i];
myarray[i] = myarray[j];
myarray[j] = temp;
}
}
}
System.out.println(myarray[n - 1]);
}
}
Step-by-step explanation:
This line declares n as length of array
int n;
Scanner input = new Scanner(System.in);
This line prompts user length of array
System.out.print("Array Length: ");
This line gets the user input
n = input.nextInt();
An empty array pf length arrowed
int [] myarray = new int[n];
This line prompts user array elements
System.out.print("Input Array: ");
The following iteration gets array element from user
for(int i=0;i<n;i++)
{
myarray[i]=input.nextInt();
}
This line declares a temporary variable temp
int temp;
This iterations iterates through the elements of rge array
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
The following if condition checks for the larger of two variables and swap their position
if (myarray[i] > myarray[j])
{
temp = myarray[i];
myarray[i] = myarray[j];
myarray[j] = temp;
}
}
}
This line prints the largest number of the array
System.out.println(myarray[n - 1]);