Java Datatypes

Sort by

recency

|

1546 Discussions

|

  • + 0 comments

    import java.util.; import java.io.;

    class Solution{ public static void main(String []argh) {

        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();
    
        for(int i=0;i<t;i++)
        {
    
            try
            {
                long x=sc.nextLong();
                System.out.println(x+" can be fitted in:");
                if(x>=Byte.MIN_VALUE && x<=Byte.MAX_VALUE)System.out.println("* byte");
                if(x>=Short.MIN_VALUE && x<=Short.MAX_VALUE )System.out.println("* short");
                if(x>=Integer.MIN_VALUE && x<=Integer.MAX_VALUE)System.out.println("* int");
                if(x>=Long.MIN_VALUE && x<=Long.MAX_VALUE)System.out.println("* long");
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }
    
        }
    }
    

    }

  • + 0 comments

    import java.util.; import java.io.; import java.math.*;

    class Solution{ public static void main(String []argh) {

        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();
    
        for(int i=0;i<t;i++)
        {
    
            try
            {
                long x=sc.nextLong();
                System.out.println(x+" can be fitted in:");
                if(x>=-128 && x<=127){
                System.out.println("* byte");
                }
                //Complete the code
                if(x>=-32768 && x<=32767){
                    System.out.println("* short");}
                if(x>= (int)Math.pow(-2,31) && x<=(int)Math.pow(-2,31) -1){
                System.out.println("* int");
                }
                if(x>=(long)Math.pow(-2,63) && x<=(long)Math.pow(-2,63) -1){
                    System.out.println("* long  ");
                }
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }
    
        }
    }
    

    }

  • + 0 comments

    import java.util.; import java.io.;

    class Solution{ public static void main(String []argh) {

        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();
    
        for(int i=0;i<t;i++)
        {
    
            try
            {
                long x=sc.nextLong();
                System.out.println(x+" can be fitted in:");
                if(x>=Byte.MIN_VALUE && x<=Byte.MAX_VALUE){
                    System.out.println("* byte");
                }
                 if(x>=Short.MIN_VALUE && x<=Short.MAX_VALUE){
                    System.out.println("* short");
                }
                 if(x>=Integer.MIN_VALUE&&x<=Integer.MAX_VALUE){
                    System.out.println("* int");
                }
                 if(x>=Long.MIN_VALUE&&x<=Long.MAX_VALUE){
                    System.out.println("* long");
                }
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }
    
        }
    }
    

    }

  • + 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();
        }
    }
    
  • + 0 comments

    I solved this problem using Java's BigInteger DataType which is ment to handle very large inputs. I believe using BigInteger is appropriate for this problem as this problem is about Java Data Types.

    import java.io.*;
    import java.util.*;
    import java.math.BigInteger;
    
    public class Solution {
        static BigInteger bn = BigInteger.valueOf(Byte.MIN_VALUE);
        static BigInteger bp = BigInteger.valueOf(Byte.MAX_VALUE);
        static BigInteger sn = BigInteger.valueOf(Short.MIN_VALUE);
        static BigInteger sp = BigInteger.valueOf(Short.MAX_VALUE);
        static BigInteger in = BigInteger.valueOf(Integer.MIN_VALUE);
        static BigInteger ip = BigInteger.valueOf(Integer.MAX_VALUE);
        static BigInteger ln = BigInteger.valueOf(Long.MIN_VALUE);
        static BigInteger lp = BigInteger.valueOf(Long.MAX_VALUE);
        
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
              Scanner sc=new Scanner(System.in);
            int loopcount=sc.nextInt();   
            
            for (int i=1;i<=loopcount;i++){
                
             BigInteger n=sc.nextBigInteger();  
             if (n.compareTo(bn)>=0 && n.compareTo(bp)<=0){
                System.out.println(n+" can be fitted in:\n* byte\n* short\n* int\n* long");} 
                 else if (n.compareTo(sn)>=0 && n.compareTo(sp)<=0){
                System.out.println(n+" can be fitted in:\n* short\n* int\n* long");}
                else if (n.compareTo(in)>=0 && n.compareTo(ip)<=0){
                System.out.println(n+" can be fitted in:\n* int\n* long");}
                else if(n.compareTo(ln)>=0 &&n.compareTo(lp)<=0){
                System.out.println(n+" can be fitted in:\n* long");}
                else
                { System.out.println(n+" can't be fitted anywhere.");}
            }
        }
    }