Java modulus operator, %, returns the remainder of a division operation.
It can be applied to floating-point types as well as integer types.
The following example program demonstrates the %:
// Demonstrate the % operator. public class Main { public static void main(String args[]) { int x = 42;//from w w w. ja v a 2 s. co m double y = 42.25; System.out.println("x mod 10 = " + x % 10); System.out.println("y mod 10 = " + y % 10); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter pairs of integers or enter to exit:"); while (input.hasNext()) { int num1 = input.nextInt(); int num2 = input.nextInt(); boolean multiple = isMultiple(num1, num2); System.out.println(multiple); }/*from w w w . ja va 2 s.c o m*/ } private static boolean isMultiple(int num1, int num2) { if (num2 % num1 == 0) return true; else return false; } }
The following code check if input is an even number.
public class Main { public static void main( String[] args ) {//from ww w. j a v a 2 s . co m java.util.Scanner input = new java.util.Scanner( System.in ); System.out.print( "Enter to number: " ); int number = input.nextInt(); System.out.printf( "The number is Even? %s%n", isEven( number ) ); } public static boolean isEven( int number ) { return number % 2 == 0; } }