-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaxMinArrayElements.java
35 lines (27 loc) · 973 Bytes
/
MaxMinArrayElements.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
/*EXP-3(B) Write a java program to find the maximum and minimum of N array elements */
import java.util.Scanner;
public class MaxMinArrayElements {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int max = arr[0];
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println("Maximum element: " + max);
System.out.println("Minimum element: " + min);
scanner.close();
}
}