HackerRank Java- Datatypes




Given an input integer, you must determine which primitive data types are capable of properly storing that input.

To get you started, a portion of the solution is provided for you in the editor.



  1. import java.util.*;
  2. import java.io.*;
  3. class Solution {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. int t = scan.nextInt();
  7. for (int i = 0; i < t; i++) {
  8. try {
  9. long x = scan.nextLong();
  10. System.out.println(x + " can be fitted in:");
  11. if (x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE) {
  12. System.out.println("* byte");
  13. }
  14. if (x >= Short.MIN_VALUE && x <= Short.MAX_VALUE) {
  15. System.out.println("* short");
  16. }
  17. if (x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE) {
  18. System.out.println("* int");
  19. }
  20. if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE) {
  21. System.out.println("* long");
  22. }
  23. } catch (Exception e) {
  24. System.out.println(scan.next() + " can't be fitted anywhere.");
  25. }
  26. }
  27. scan.close();
  28. }
  29. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube