-4
System.out.println("30%4 = " + 30%4);
System.out.println("33%4 = " + 33%4);

System.out.println("30*4/100 = " + 30*4/100);
System.out.println("33*4/100 = " + 33*4/100);

My Question is why 2 not 1 in 30%4 case. Can anybody explain me in details. Thanks in Advance

Nafis Md
  • 3
  • 1

4 Answers4

2

You have to understand how modulus works. =) when you apply modulus it gets divided and returns the "remainder"...

How Does Modulus Divison Work there is a very good explanation already posted.

Community
  • 1
  • 1
Aven
  • 72
  • 10
2
30 % 4 = 2 

because:

4 + 4 + 4 + 4 + 4 + 4 + 4 is 28 so 30 -28 = 2

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
SWiggels
  • 2,159
  • 1
  • 21
  • 35
0

In Java, integer division returns the floor of the computation (rounds down to the nearest integer). This is why 30*4/100 comes out to 1.

On the other hand, modulus (%) is the remainder of integer division. Since 28 is divisible by 4, the remainder of 30/4 would be 2 (as 28+2=30).

Amit
  • 64
  • 2
0

The % operator is modulo, it takes the remainder after division of the two numbers, in your case it divides 30 by 4, the highest multiple is 7 as 7*4 = 28 now the remainder is 2, which is your answer, for the 33 case it does the same, 8*4 = 32 remainder is 1