In: Computer Science
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is between 100 and 1000, not including the 1000 number greater than 1000 -- if it is greater than 1000
// WhereIsMyNumber.java
import java.util.Scanner;
public class WhereIsMyNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
// As per condition it might contain decimal points also
float n = sc.nextFloat();
if(n < 0) {
// if number is less than zero then below will be printed
System.out.println("Number is negetive");
}else if( n >= 0 && n < 10) {
// if number is in between 0 and 10 excluding 10
// below statement will be printed
System.out.println("Number is [0,10)");
}else if( n >= 10 && n < 100 ) {
// if number is in between 10 and 100 excluding 100
// below statement will be printed
System.out.println("Number is [10,100)");
}else if( n >= 100 && n < 1000 ) {
// if number is in between 100 and 1000 excluding 1000
// below statement will be printed
System.out.println("Number is [100,1000)");
}else if( n >= 1000 ) {
// if number is greater than or equal to 1000
// below statement will be printed
System.out.println("Number is greater than 1000");
}
sc.close();
}
}
Outputs:
Testcase #1
Testcase #2
Testcase #3
Testcase #4
Testcase #5