Java Datatypes

  • + 0 comments
    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            String[] types = new String[]{"* byte", "* short", "* int"};
            for (int i = 0; i < t; i++) {
                try {
                    long x = sc.nextLong();
                    System.out.println(x + " can be fitted in:");
                    for (int k = 0; k < 3; k++) {
                        double exponent = Math.pow(2, k + 3); // 8 bit, 16 bit, 32 bit
                        if (x >= -Math.pow(2, exponent - 1) && x <= Math.pow(2, exponent - 1) - 1) {
                            System.out.println(types[k]);
                        }
                    }
                    System.out.println("* long");
                } catch (Exception e) {
                    System.out.println(sc.next() + " can't be fitted anywhere.");
                }
    
            }
            sc.close();
        }
    }