We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Java
  3. Introduction
  4. Java Datatypes
  5. Discussions

Java Datatypes

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1284 Discussions, By:

recency

Please Login in order to post a comment

  • arif_erol16
    14 hours ago+ 0 comments

    import java.util.*;

    class Solution {

    public static void main(String[] argh) {
        validateNumber();
    }
    
    private static void validateNumber() {
        Scanner sc = new Scanner(System.in);
        int t = 0;
        try {
            t = sc.nextInt();
            for (int i = 0; i < t; i++) {
                try {
                    long x = sc.nextLong();
                    if (x >= -9223372036854775808L && x <= 9223372036854775807L) {
                        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 > -9223372036854775808l && x <= 9223372036854775807l) {
                            System.out.println("* long");
                        }
                    }
                } catch (Exception e) {
                    System.out.println(sc.next() + " can't be fitted anywhere.");
                }
            }
        } catch (Exception e) {
            System.out.println(sc.next() + " Error number please retry.");
            validateNumber();
        }
        sc.close();
    }
    

    }

    0|
    Permalink
  • meghatalathi02
    1 day ago+ 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>=-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>= -9223372036854775808L && x<= 9223372036854775807L)         System.out.println("* long");
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }
    
        }
    }
    

    }

    0|
    Permalink
  • kondurlavanya11
    5 days ago+ 0 comments

    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");
                }
                if (x >= -9223372036854775808L && x <= 9223372036854775807L) {
                    System.out.println("* long");
                }
            } catch (Exception e) {
                System.out.println(sc.next() + " can't be fitted anywhere.");
            }
        }
    }
    
    0|
    Permalink
  • sshujaat23
    5 days ago+ 0 comments

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

    public class Solution {

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

    }

    0|
    Permalink
  • phamvandat_hcmut
    6 days ago+ 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>=-128 && x<=127)System.out.println("* byte");
                    //Complete the code
                    if( x >= -Math.pow(2,15) && x <= Math.pow(2,15)-1)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|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy