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.

(int) Math.pow(2, 31) casting would be done before the subtraction I believe. Do you need to cast?
also, you really don't need to check for long. Any value not throwing an exception will fit.

I think for loop is troubling everyone here (including me ). For loop limit should be the first int we are taking . Its just not any random number, its actually the number of inputs which different test cases gonna have.

use (short)(Math.pow(2,15)-1) instead of (short) Math.pow(2,15) -1 because:

The value of Math.pow(2,15) = 32768.0 ,when you type cast it to short its value becomes -32768 and when you subtract it from 1 the value is -32769 .So,first subtract 1 from the Math.pow(2,15) then type cast to short.The same logic for the int and long.

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");

any of the data type has a specific range.
take byte!
its range is from -128 to 127;
if you raise - 2 to the power of 7 i.e. -128. (this number is in the required range!).
but if you raise 2 to the power of 7 i.e. 128. (this number is not in the required range(127)). So, all you need is subtract 1 from 128 and hence the code :

long 64 –9,223,372,036,854,775,808 to
9 ,223,372,036,854,775,807

int 32 –2,147,483,648 to
2,147,483,647

short 16 –32,768 to
32,767

byte 8 –128 to
127

if you recognize the positive number of the primitive variable is alway less with 1, check the Byte the minimum is -128 and the positive is not 128, its 128 -1 = 127, so we add -1 so we can have the right positive range and have the right value.
wish i was helpfull

Thanks for your reply.
I'm aware of the data type ranges.
Simply just inserting the ranges within an if statements isn't enough and doesn't pass you the test cases 3 & 4.

Nevertheless, I've managed to solve the challenge! :D

hey dear Smiriti there is no need math.pow() fuction as u can see there is long in all cases except can't fitted else condition so use simply this code
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");
System.out.println("* long");
//Complete the code
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
}

I just have one silly question. Say we are doing for short then I wanted to know why we are doing Math.pow(2,15) and not Math.pow(2,16) considering we want 2 to the power of 16 and not 15.

## Java Datatypes

You are viewing a single comment's thread. Return to all comments →

Hey Kucao, I had the same solution as yours, with just this minor change, and I got it wrong. Not able to understand why. Can someone explain ?

casting will truncate value

but here it every casting is in range its range we are dealing with int types so where comes the question of casting

That messed me up. I casted and was not able to get the as long value. Once I remove it worked.

We do not need to cast explicitly. We are just required to check the applicable ranges.

i tried using appying the range directly but i couldnt able to clear two test cases

which?

u added pow function but i tried with entering the range values like for byte -128 to 127 but i am not able to clear the 2 testcases among 4

but then..i thnk this wont work?

Thanks...

Casting is necessary because Math.pow function returns value in double

(int) Math.pow(2, 31) casting would be done before the subtraction I believe.

Do you need to cast?

also, you really don't need to check for long. Any value not throwing an exception will fit.

use x<= (short) (Math.pow(2,15)-1.0)

Simply write your own 2^x function and use that instead of Math.pow which has an upper limit:

Nice

I think for loop is troubling everyone here (including me ). For loop limit should be the first int we are taking . Its just not any random number, its actually the number of inputs which different test cases gonna have.

same here

same

use (short)(Math.pow(2,15)-1) instead of (short) Math.pow(2,15) -1 because:

The value of Math.pow(2,15) = 32768.0 ,when you type cast it to short its value becomes -32768 and when you subtract it from 1 the value is -32769 .So,first subtract 1 from the Math.pow(2,15) then type cast to short.The same logic for the int and long.

Agreed. That makes sense.

not running

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");

no no impossible

We can reply a redundant condition check through the below mentioned code:

try{ long x=sc.nextLong(); System.out.println(x+" can be fitted in:"); if(x>=Byte.MIN_VALUE && x<=Byte.MAX_VALUE)System.out.println("* byte\n* short\n* int\n* long"); else if(x>=Short.MIN_VALUE && x<=Short.MAX_VALUE)System.out.println("* short\n* int\n* long"); else if(x>=Integer.MIN_VALUE && x<=Integer.MAX_VALUE)System.out.println("* int\n* long"); else System.out.println("* long"); }catch(Exception ex){ System.out.println(sc.next()+" can't be fitted anywhere."); }

Why are we including -1 at the end. can anyone explain me?

noo

because of range

can you explain it clearly please

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

any of the data type has a specific range. take byte! its range is from -128 to 127; if you raise - 2 to the power of 7 i.e. -128. (this number is in the required range!). but if you raise 2 to the power of 7 i.e. 128. (this number is not in the required range(127)). So, all you need is subtract 1 from 128 and hence the code :

bacause, like byte range is 8 so last in 0 to 7 meanse 8-1=7 hope you got it

It's to do with the range that data type can have.

For example short can have minimum -32,768 to maximum of 32,767.

Same case for int & long, which is ONE less number for it's maximum.

It's easier to write (2^31 - 1) than it is to write 2147483648 - 1 = 2147483647

long 64 –9,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807

int 32 –2,147,483,648 to 2,147,483,647

short 16 –32,768 to 32,767

byte 8 –128 to 127

Thanks for your reply. I'm aware of the data type ranges. Simply just inserting the ranges within an if statements isn't enough and doesn't pass you the test cases 3 & 4.

Nevertheless, I've managed to solve the challenge! :D

Because in range of every databytes from negative to positive(postive is (-1) less than to negative ) as in byte -128 to +127.

If first if condition work then no other if will work.

(short)-Math.pow(2, 15)What is the use of minus sign '-' there???short range of value lie to (-2 ^15 to 2^15-1)

According to problem statement the long data type can fit in all possibe input except when it doesnt. So the initialization and assignment part

hey,kinara.....please explain me the needs for type casting every Math.pow().....??

yes here's need of this function

hey dear Smiriti there is no need math.pow() fuction as u can see there is long in all cases except can't fitted else condition so use simply this code import java.util.

; import java.io.;class Solution{ public static void main(String []argh) {

}

I just have one silly question. Say we are doing for short then I wanted to know why we are doing Math.pow(2,15) and not Math.pow(2,16) considering we want 2 to the power of 16 and not 15.

this is impisive casting of primitive types.that happen automatically.compiler does it when ever requary.