In: Computer Science
I need it in java.
Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times asking for an integer number and for each number that the user input it will present whether the number is or not within the Min-Max range (including their respective value). As an example, if the user inputs: "3 2 5 8 4 2" the output would be:
8 is not within the range between 2 and 5 4 is within the range between 2 and 5 2 is within the range between 2 and 5
Solution for the given question are as follows -
Code :
import java.util.*;
public class Demo
{
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
// local variable declaration
int n, min, max;
// get inputs from user
System.out.println("How many
elements you want :");
n = sc.nextInt();
System.out.println("Enter min and
max range :");
min = sc.nextInt();
max = sc.nextInt();
int []a = new int[n];
System.out.println("Enter "+ n + "
elements : ");
for (int i = 0; i< n ; i++ )
{
a[i] = sc.nextInt();
}
// check number is between min and
max
for (int i = 0; i< n ; i++ )
{
if (a[i] >= min && a[i]
<= max) {
System.out.println(a[i]+ " is
within the range between "+ min + " and "+ max);
} else {
System.out.println(a[i]+ " is not
within the range between "+ min + " and "+ max);
}
}
}
}
Code Screen Shot :
Output :