-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxofArray.java
More file actions
38 lines (27 loc) · 936 Bytes
/
MaxofArray.java
File metadata and controls
38 lines (27 loc) · 936 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
38
import java.*;
import java.util.Scanner;
public class MaxofArray {
public static void main(String[] args) {
//This is used when array is predifined
int[] arr = {4,5,1,7,9,10,6,7,11};
int max = arr[0];
for(int i = 0; i < arr.length; i++){
if(arr[i]>max)
max=arr[i];
}
System.out.println(max);
//This code is used when array size and array is provided by the user
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int abc[] = new int[n] ;
for(int i = 0; i < n; i++){
abc[i] = sc.nextInt();
}
int maxi = abc[0];
for(int j = 0; j < abc.length; j++){
if(abc[j]>maxi)
maxi = abc[j];
}
System.out.print("Max element " + maxi);
}
}