BufferedReader reader= new BufferedReader(new InputStreamReader(System.in)); String a=reader.readLine(); int b=Integer.parseInt(a); double c=b/4; // double d=c-(int)c; System.out.print(c); 

For example, when you enter the number 13, it gives the answer 3.0

  • 2
    because when dividing integers, the result is an integer. - KoVadim
  • ... and judging by the number of such questions, this is a standard developers error. - VladD

1 answer 1

Because b is an int and 4 is also an int String double c = b / 4; works like this:

 double c = (double)(b / 4); 

First there is an integer division, and then reduction to double ;

To make the division not integer write:

 double c = b / 4.0; 

In this case, b will be double to double everything will be correct.