forked from vishal8113/Hacktoberfest-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
35 lines (28 loc) · 1020 Bytes
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create a Scanner object for input
Scanner sc = new Scanner(System.in);
// Ask for the number of elements in the array
System.out.print("Enter the number of elements in the array: ");
int n = sc.nextInt();
// Initialize the array
int[] arr = new int[n];
// Read the array elements from the user
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Find the largest element
int largest = arr[0]; // Assume the first element is the largest
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
// Print the largest element
System.out.println("The largest element in the array is: " + largest);
// Close the scanner
sc.close();
}
}