What is the output of the following program
public class Main { public static void main(String args[]) { System.out.println(5 / 4); System.out.println(10 / 4); } }
1 2
In Java, if you divide two integers, the result is an integer.
The fractional part is truncated.
For example, 5 / 4 is 1 (not 1.25) and 10 / 4 is 2 (not 2.5).
To get an accurate result, make sure that one of the values involved in the division is a number with a decimal point.
For example, 5.0 / 4 is 1.25 and 10 / 4.0 is 2.5.