-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLargestElementInArray.java
More file actions
39 lines (36 loc) · 1.09 KB
/
Copy pathLargestElementInArray.java
File metadata and controls
39 lines (36 loc) · 1.09 KB
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
38
39
package Java_basic_Programs;
import java.util.Scanner;
public class LargestElementInArray {
public static int max(int[] input){
int max = Integer.MIN_VALUE;
for(int i=0 ; i<input.length ; i++){
if(input[i] > max){
max = input[i];
}
}
return max;
}
public static int[] takeInput(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size:");
int size = sc.nextInt();
int[] input = new int[size];
System.out.println("Enter the elements:");
for(int i=0 ; i<size ; i++){
input[i] = sc.nextInt();
}
return input;
}
public static void printArray(int[] input){
for(int a : input){
System.out.print(a+" ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = takeInput();
printArray(arr);
int largest = max(arr);
System.out.println("The largest element in the array is : "+largest);
}
}