I want to take input of number from the user and check if the number can be fit in byte, short, int, long datatypes, if user inputs extreme large number , I just display that the entered number cannot be fit in any datatype.
import java.lang.Math;
import java.util.Scanner;
class Zcheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String l = sc.next();
if(l >= -128 && l <= 127)
System.out.println(l+" can be fitted in:\n* byte\n* short\n* int\n* long");
else if(l >= -32768 && l <= 32767)
System.out.println(l+" can be fitted in:\n* short\n* int\n* long");
else if(l >= -Math.pow(2, 31) && l <= (Math.pow(2, 31) - 1))
System.out.println(l+" can be fitted in:\n* int\n* long");
else if(l >= -Math.pow(2, 63) && l <= (Math.pow(2, 63) - 1))
System.out.println(l+" can be fitted in:\n* long");
else
System.out.println(l+" can't be fitted anywhere.");
}
}
}
Things were okk when the user inputs a number which can be store in long type I just make long variable and comparing it with the with the ranges of datatypes and display the appropriate message using if-else. But when the user inputs extremly large number it was the problem that I cannot store such large number in long data type too. So I tried for String or BigInteger data types but further the problem occurred that I cannot compare String or BigInteger with the another datatype in if-else condition.