Arithmetic operations | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Arithmetic operations

Why answer is 2? int x = 19; int y = 7; System.out.print(x/y%3)

4th Mar 2020, 7:12 PM
Терещенко Дмитрий
Терещенко Дмитрий - avatar
2 Answers
+ 7
Let's break it into pieces: / and % have same precedence. So we start from left to right. x/y == 19 / 7 == 2.71..... But both x and y are integers. Division (/) operator must return an int when both operands are ints. 2.71... is truncated to 2. Now 2 % 3. % is the remainder operator. 2/3 is 0 remainder 2. Answer is 2
4th Mar 2020, 7:30 PM
Kevin ★
+ 1
thx
4th Mar 2020, 7:32 PM
Терещенко Дмитрий
Терещенко Дмитрий - avatar