Integer Division
Completed
Print 7 / 2, 7 / 2.0, 7 % 2 and (double) 7 / 2, one per line.
Input
None.
Output
Four lines.
Example
Output
3 3.5 1 3.5
Editor
Java
Hints
int / int gives an int - the fraction is dropped.
If either side is a double, the result is a double.
Solution explanation
Integer division truncates. Casting one operand to double gives a decimal result.
Show solution
public class Main {
public static void main(String[] args) {
System.out.println(7 / 2);
System.out.println(7 / 2.0);
System.out.println(7 % 2);
System.out.println((double) 7 / 2);
}
}