-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
49 lines (40 loc) · 1.44 KB
/
Main.java
File metadata and controls
49 lines (40 loc) · 1.44 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
40
41
42
43
44
45
46
47
48
49
package findNearestNumberWithArray;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author Fatih ARI - 23.08.2021
*
* Using Arrays, a program that finds the closest number smaller than
* the entered number and the closest closest number greater than the
* entered number is designed.
*
*/
public class Main {
public static void main(String[] args) {
clearScreen();
Scanner input = new Scanner(System.in);
int[] array = { 15, 12, 788, 1, -1, -778, 2, 0 };
Arrays.sort(array);
System.out.println(Arrays.toString(array));
System.out.print("Enter a number: ");
int number = input.nextInt();
int nearestSmallerNumber = array[0];
int nearestGreaterNumber = array[0];
for (int i : array) {
if (i < number)
nearestSmallerNumber = i;
if (i > number) {
nearestGreaterNumber = i;
break;
}
}
System.out.println("The nearest number smaller than the entered number is: " + nearestSmallerNumber);
System.out.println("The nearest number greater than the entered number is: " + nearestGreaterNumber);
input.close();
}
// It is used for console screen cleaning in Java.
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}