-
Notifications
You must be signed in to change notification settings - Fork 110
/
Large_Small.java
42 lines (36 loc) · 1.18 KB
/
Large_Small.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
36
37
38
39
40
41
42
/*
Write a program that reads in a list of integers and prints out the smallest and largest integer in the list.
Your program should first ask the user to enter the number of integers they want to input. Then it should prompt the user to
enter each integer one by one.
After all the integers have been entered, the program should output the smallest and largest integer in the list.
Input
-------------------------
Enter the number of integers: 5
Enter integer 1: 10
Enter integer 2: -5
Enter integer 3: 20
Enter integer 4: 0
Enter integer 5: 15
Output
-------------------------
Smallest integer: -5
Largest integer: 20
*/
import java.util.*;
public class Large_Small
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of integers: ");
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
System.out.print("Enter integer "+(i+1)+": ");
arr[i] = sc.nextInt();sc.nextLine();
}
Arrays.sort(arr);
System.out.println("Smallest integer: "+arr[0]);
System.out.println("Largest integer: "+arr[n-1]);
}
}