I have a small problem, but I hope somebody can help.
Let’s say that I have an array of integers, something like this:
int[] = {65, 75, 85}
And the program takes an integer input from the user, and the output will be the closest number from the array.
For example: If the user inputs 68, the output will be 65, since 65 is the closest number. Or if the user inputs 100 the output will be 85, since 85 is the closest.
EDIT: This is NOT homework. I program for myself not for anyone else 😛
Thanks in advance,
Martin
The important thing is to compare the absolute differences.
Try this:
This code will return
0if the array is empty.Note: It is possible the input and array element is more than
Integer.MAX_VALUEapart. You may want to code for this.