-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestElementFinder.java
More file actions
37 lines (29 loc) · 984 Bytes
/
LargestElementFinder.java
File metadata and controls
37 lines (29 loc) · 984 Bytes
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
36
37
import java.util.Scanner;
public class LargestElementFinder {
// Method to find the largest element in an array
public static int largestElement(int[] nums) {
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
return max;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input the size of the array
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] nums = new int[n];
// Input the array elements
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
// Find and print the largest element
int largest = largestElement(nums);
System.out.println("The largest element is: " + largest);
sc.close();
}
}