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.
Kucao's solution is NOT exactly correct.
What if the input is number = 10.
Number 10 can be fit into ANY primitive type. So, every single "if" in Kucao's code will be duplicated in the output.
Better to use if-else if construct. Like this
longx=sc.nextLong();System.out.println(x+" can be fitted in:");if(x>=-128&&x<=127)System.out.println("* byte\n* short\n* int\n* long");elseif(x>=((-1)*(Math.pow(2,15)))&&x<=((Math.pow(2,15)-1)))System.out.println("* short\n* int\n* long");elseif(x>=((-1)*(Math.pow(2,31)))&&x<=((Math.pow(2,31)-1)))System.out.println("* int\n* long");elseif(x>=((-1)*(Math.pow(2,63)))&&x<=((Math.pow(2,63)-1)))System.out.println("* long");
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Datatypes
You are viewing a single comment's thread. Return to all comments →
Kucao, and all
Kucao's solution is NOT exactly correct. What if the input is number = 10. Number 10 can be fit into ANY primitive type. So, every single "if" in Kucao's code will be duplicated in the output.
Better to use if-else if construct. Like this