Java – why is only integer part written in c?

Question:

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

Answer:

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

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

First, there is an integer division, and then a cast 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.

Scroll to Top