Java Datatypes

Sort by

recency

|

1575 Discussions

|

  • + 0 comments

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

    public class Solution {

    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 scan = new Scanner(System.in);
        int t = scan.nextInt();
        boolean typeFit[] = {false,false,false,false};
        boolean fit = false;
        String types[] = {"* byte","* short","* int","* long"};
        scan.nextLine();
        for (int i=0; i<t; i++) {
            String sample = scan.nextLine();
            long sampleLong = 0l;
            try {
                System.out.print(sample);
                sampleLong = Long.parseLong(sample);
                if(sampleLong >=-128 && sampleLong <= 127) {
                    typeFit[0]=true;
                    fit=true;
                }
                if(sampleLong >=-32768 && sampleLong<= 32767) {
                    typeFit[1]=true;
                    fit=true;
                }
                if(sampleLong >=Math.pow(-2,31) && sampleLong<= Math.pow(2,31)-1) {
                    typeFit[2]=true;
                    fit=true;
                }
                if(sampleLong >=Math.pow(-2,63) && sampleLong<= Math.pow(2,63)-1) {
                    typeFit[3]=true;
                    fit=true;
                }
            } catch(Exception e) {
                System.out.println(" can't be fitted anywhere.");
    
            }
            if(fit)
               System.out.println(" can be fitted in:");
            for(int c =0; c<4;c++) {
                if(typeFit[c]) {
                    System.out.println(types[c]);
                    typeFit[c]=false;
                }
            }
            fit=false;
        }
    }
    

    }

  • + 0 comments

    Java is such a powerful and versatile programming language—perfect for everything from building enterprise applications to Android development. Its platform independence and strong community support make it a solid choice for beginners and pros alike! Cricbet99 Login ID and Password

  • + 0 comments

    Here is Java Datatypes solution - https://programmingoneonone.com/hackerrank-java-datatypes-problem-solution.html

  • + 1 comment

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

    class Solution { public static void main(String []args) { 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 >= -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.next() + " can't be fitted anywhere.");
            }
        }
    }
    

    }

  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
        int numeroCasoTeste = scan.nextInt();
        for (int i = 0; i < numeroCasoTeste; i++) {
            if (scan.hasNextLong()) { 
                long numero = scan.nextLong();
                System.out.println(numero + " can be fitted in:");
                if ((numero >= Byte.MIN_VALUE) && (numero <= Byte.MAX_VALUE))
                    System.out.println("* byte");
                if ((numero >= Short.MIN_VALUE) && (numero <= Short.MAX_VALUE))
                    System.out.println("* short");
                if ((numero >= Integer.MIN_VALUE) && (numero <= Integer.MAX_VALUE))
                    System.out.println("* int");
               if ((numero >= Long.MIN_VALUE) && (numero <= Long.MAX_VALUE))
                    System.out.println("* long");
            }else {
                String valorNaopermitido = scan.next(); 
                System.out.println(valorNaopermitido + " can't be fitted anywhere.");
            }
        }
        }
    }