0

I have some confusion on the modulus operator, modulus gives the remainder as the output. so why is it different for the below scenario?

public class ModulusTest{
       public static void main(String[] args) {
        
         System.out.println(" 7/10 : " + 7/10 );//0.7
         System.out.println(" 7%10 : " + 7%10 ); //0
         
         System.out.println(" 5/2 : " + 5/2 );//2
         System.out.println(" 5%2 : " + 5%2 );//1
}
}

The output i was expecting is as i kept in the comments on each line of the print statements in the above sample code. output:

 7/10 : 0
 7%10 : 7
 5/2 : 2
 5%2 : 1
user222
  • 544
  • 3
  • 9
  • 28
  • Integer division – Michael Jul 27 '20 at 22:29
  • 1
    Why would you expect the remainder of 7÷10 to be zero? – chrylis -cautiouslyoptimistic- Jul 27 '20 at 22:33
  • Good explanation here https://stackoverflow.com/questions/17524673/understanding-the-modulus-operator – user176692 Jul 27 '20 at 22:35
  • when we divide 7 with 10, the remainder will be zero and quotient is 0.7 (mathematically). – user222 Jul 27 '20 at 22:36
  • 2
    But you get 5/2 right -- why did you not think the quotient would be 2.5 and the remainder 0 there? What difference do you perceive? (I say 'perceive' because there is no difference - in both cases it's integer division with an integer remainder). – user13784117 Jul 27 '20 at 22:46
  • 1
    It's not the "modulus operator", it's the ["remainder operator"](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3) -- the 'remainder' for positive integers being pretty much how it was defined in primary school: 7 'goes into' 10 no times, with a remainder of 7. – user13784117 Jul 27 '20 at 22:50

0 Answers0