Java Datatypes

Sort by

recency

|

1542 Discussions

|

  • + 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.");}
            }
        }
    }
                     
                
            
    
  • + 0 comments
    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
                {
                    // -32,768 to 32,767
    				// -2,147,483,648 to 2,147,483,647
    			//  -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
                   
    							 ong x=sc.nextLong();
                    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>=-2147483648 && x<=2147483647)System.out.println("* int");
                    if(x>=-9223372036854775808.0 && x <= 9223372036854775807.0)System.out.println("* long");
                    //Complete the code
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }
    
            }
        }
    }
    
    
    
    # 
    
    
    
  • + 0 comments
    import java.util.Scanner;
    
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int t = scan.nextInt();
            
            while (t-- > 0) {
                try {
                    long n = scan.nextLong();
                    solve(n);
                } catch(Exception e) {
                    System.out.println(scan.next() + " can't be fitted anywhere.");
                }
            }
            
            scan.close();
        }
        
        static void solve(long n) {
            System.out.println(n + " can be fitted in:");
            if (n >= Byte.MIN_VALUE && n <= Byte.MAX_VALUE)
                System.out.println("* byte");
            
            if (n >= Short.MIN_VALUE && n <= Short.MAX_VALUE)
                System.out.println("* short");
                
            if (n >= Integer.MIN_VALUE && n <= Integer.MAX_VALUE)
                System.out.println("* int");
                
            if (n >= Long.MIN_VALUE && n <= Long.MAX_VALUE)
                System.out.println("* long");
        }
    }
    
  • + 0 comments

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

    class Solution{

    public static long power(int a, int b)
    {
        long n=1;
        do
        {
                   n=n*a;     
            b--;
        }
        while(b>0);
        return n;
    }
    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>=-power(2,7) && x<=power(2,7)-1) System.out.println("* byte");
                //Complete the code
    
                if(x>=-power(2,15) && x<=power(2,15)-1) System.out.println("* short");
    
                if(x>=-power(2,31) && x<= power(2,31)-1)
                {
                    System.out.println("* int");
                }
    
                if(x>=-power(2,63) && x<=power(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 n=sc.nextLong();
                System.out.println(n+" can be fitted in:");
                if (n >= Byte.MIN_VALUE && n <= Byte.MAX_VALUE) {
                    System.out.println("* byte");
                }
                if (n >= Short.MIN_VALUE && n <= Short.MAX_VALUE) {
                    System.out.println("* short");
                }
                if (n >= Integer.MIN_VALUE && n <= Integer.MAX_VALUE) {
                    System.out.println("* int");
                }
                if (n >= Long.MIN_VALUE && n <= Long.MAX_VALUE) {
                    System.out.println("* long");
                }
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }
    
        }
    }
    

    }