Showing posts with label Remainder. Show all posts
Showing posts with label Remainder. Show all posts

Monday, September 24, 2012

Modulo %

Download the code

This operator returns the remainder of division of one number by another number.


Integer examples
For instance, 10 / 2 has a remainder of 0. This is because 10 is divided by 2, exactly.
Another example, 15 / 2 has a remainder of 1. This is because, after division has taken place (15 can be divided 7 times), 1 is left over.
More examples:

The remainder of an integer division only returns the integer part of the result.
For instance, if you were to type 4 / 3 into a calculator, you would see the result is 1.3333333 etc. However, where this calculation is being performed as integer division in a programming language, such as java, only the integer part is returned - producing the result 1.


Tuesday, January 31, 2012

Remainder

"The Remainder or Modulus Operator in Java" ( http://bit.ly/zOQRrd )

For instance 10 % 3 is 1 because 10 divided by 3 leaves a remainder of 1.

int i = 10;
int j = 3;

System.out.println("i is " + i);
System.out.println("j is " + j);
  
int k = i % j;
System.out.println("i%j is " + k);
 
% javac Remainder.java
% java Remainder
i is 10
j is 3
i%j is 1