Java Datatypes

Sort by

recency

|

1537 Discussions

|

  • + 0 comments

    ` import java.util.*;

    class Solution{ public static void main(String []argh) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i println(x+" can be fitted in:");

                if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE) println("* byte");
                if(x >= Short.MIN_VALUE && x <= Short.MAX_VALUE) println("* short");
                if(x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE) println("* int");
                if(x >= Long.MIN_VALUE && x <= Long.MAX_VALUE) println("* long");
            }
            catch(Exception e)
            {
                println(sc.next()+" can't be fitted anywhere.");
            }
        }
    }
    public static void println (String value) {
        System.out.println(value);
    }
    

    } `

    `

  • + 0 comments

    public class Sollution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int inputs = scan.nextInt();
    
        scan.nextLine();
        BigInteger longmax = BigInteger.valueOf(Long.MAX_VALUE);
        BigInteger longmin = BigInteger.valueOf(Long.MIN_VALUE);
        BigInteger intmax=  BigInteger.valueOf(Integer.MAX_VALUE);
        BigInteger intmin = BigInteger.valueOf(Integer.MIN_VALUE);
        BigInteger shortmin=  BigInteger.valueOf(Short.MIN_VALUE);
        BigInteger shortmax=  BigInteger.valueOf(Short.MAX_VALUE);
        BigInteger bytemin=  BigInteger.valueOf(Byte.MIN_VALUE);
        BigInteger bytemax=  BigInteger.valueOf(Byte.MAX_VALUE);
    
        for(int i=0;i<inputs;i++){
            String numtostr = scan.nextLine();
            boolean fits= false;
            try{
                BigInteger bignum = new BigInteger(numtostr);
                if(bignum.compareTo(bytemax)<=0 && bignum.compareTo(bytemin)>=0){
                    if(!fits) 
                        System.out.println(numtostr+" can be fitted in: ");
    
                    System.out.println(" byte");
                    fits= true;
                }
                if(bignum.compareTo(shortmax)<=0 && bignum.compareTo(shortmin)>=0){
                    if(!fits) 
                        System.out.println(numtostr+" can be fitted in: ");
    
                    System.out.println(" short"); 
                    fits = true;
                }
                if(bignum.compareTo(intmax)<=0 && bignum.compareTo(intmin)>=0){
                    if(!fits) 
                        System.out.println(numtostr+" can be fitted in: ");
    
                    System.out.println(" integer");
                    fits = true;
                }
                if(bignum.compareTo(longmax)<=0 && bignum.compareTo(longmin)>=0){
                    if(!fits)
                        System.out.println(numtostr+" can be fitted in: ");
    
                    System.out.println(" long");
                    fits= true;
                }
            }
            catch(Exception E){
                if(!fits)
                System.out.println(numtostr+" can't be fitted anywhere in int,short, long");    
    

    /213333333333333333333333333333333333- only this o/p is getting wrong/ } } scan.close(); } }

  • + 0 comments
     try
                {
                    long x=sc.nextLong();
                    System.out.println(x+" can be fitted in:");
                    if(x>=-128 && x<=127)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");
                    //Complete the code
                }
    
  • + 0 comments

    my code is correct and its executing properly but here its showing as wrong output,why?

    import java.util.Scanner;

    class Solution{ public static void main(String []args) { code is right why the o/p is wrong its showing 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");}
    
                if(x>=-32768 && x<=32767)
                {System.out.println("* short");}
    
               if(x>=-2147483648 && x<=2147483647)
                {System.out.println("* int");
                }
                System.out.println("* long");
                //Complete the code
            }
            catch(Exception e)
            {
                System.out.println(sc.next() + " can't be fitted anywhere");
            }
    
        }
    }
    

    }

  • + 0 comments

    Scanner sc = new Scanner(System.in);

        // Read the number of test cases
        int t = sc.nextInt();
    
        // Process each test case
        for (int i = 0; i < t; i++) {
            try {
                // Read the number as a long (to handle very large values)
                long n = sc.nextLong();
    
                // Start the output with the number that will be checked
                System.out.println(n + " can be fitted in:");
    
                // Check each type and print if it can fit in the respective type
                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) {
                // If the number is too large or too small, it can't be fitted
                System.out.println(sc.next() + " can't be fitted anywhere.");
            }
        }
    
        // Close the scanner after use
        sc.close();
    

    }