Java program to check whether a given integer can be stored in Java Integer Data Types - HackerRank


 

// Program by Akash Tripathi (@proakash256) 
import java.io.*;
public class Java_Data_type {
    public static void main(String[] argsthrows IOException
    {
        BufferedReader sc = new BufferedReader 
(new InputStreamReader(System.in));
        long t = Long.parseLong(sc.readLine());
        for(int i = 1i <= ti = i + 1)
        {
            try
            {
                long x = Long.parseLong(sc.readLine());
                System.out.println(x+" can be fitted in:");
                if(x>=-128 && x<=127)
                    System.out.println("* byte");
                if(x>=-32768 && x<=32767)
                    System.out.println("* short");
                if(x >= -(Math.pow(2 , 31)) &&
x <= (Math.pow(2 , 31) - 1))
                    System.out.println("* int");
                if(x >= -(Math.pow(2 , 63)) &&
x <= (Math.pow(2 , 63) - 1))
                    System.out.println("* long");
            }
            catch(Exception e)
            {
                System.out.println(sc.read()
+" can't be fitted anywhere.");
            }
        }
    }
}

Comments