Borislav Hadzhiev
Oct 15, 2021·1 min read
Last updated: Oct 15, 2021
To check if one number is a multiple of another number, use the modulo operator %. The operator returns the remainder when one number is divided by another number. The remainder will only be zero if the first number is a multiple of the second.
index.js
Copied!
const num1 = 15; const num2 = 5; const remainder = num1 % num2; console.log(remainder); // 👉️ 0 if (remainder === 0) { console.log('👉️ num1 multiple of num2'); } else { console.log('⛔️ num1 NOT multiple of num2'); }In the example we use the modulo operator to get the remainder of the division of 2 numbers.
If the remainder after dividing the first by the second number is `0`, we know that the first number is a multiple of the second.
Here are some more examples of using the modulo operator.
index.js
Copied!
console.log(13 % 5); // 👉️ 3 console.log(12 % 5); // 👉️ 2 console.log(11 % 5); // 👉️ 1 console.log(10 % 5); // 👉️ 0In the example 10 is a multiple of 5, therefore the division has a remainder of 0.
The modulo operator always takes the sign of the first number (the dividend).
index.js
Copied!
console.log(-13 % 5); // 👉️ -3 console.log(12 % -5); // 👉️ 2You might be wondering what happens if we get a remainder of -0. Would our if condition fail because we only check for 0.
index.js
Copied!
console.log(-15 % 3); // 👉️ -0However, 0 is equal to -0 in JavaScript, which means our condition would still work.
index.js
Copied!
console.log(0 === -0); // 👉️ true
This is a Java Program to Accept two Integers and Check if they are Equal.
Enter two integer numbers as input. After that we match both the inputs with the help of equal to(==) operator. Hence we generate the output accordingly.
Here is the source code of the Java Program to Accept two Integers and Check if they are Equal. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
- import java.util.Scanner;
- public class Equal_Integer
- {
- public static void main(String[] args)
- {
- int m, n;
- Scanner s = new Scanner(System.in);
- System.out.print("Enter the first number:");
- m = s.nextInt();
- System.out.print("Enter the second number:");
- n = s.nextInt();
- if(m == n)
- {
- System.out.println(m+" and "+n+" are equal ");
- }
- else
- {
- System.out.println(m+" and "+n+" are not equal ");
- }
- }
- }
Output:
$ javac Equal_Integer.java $ java Equal_Integer Enter the first number:5 Enter the second number:7 5 and 7 are not equal Enter the first number:6 Enter the second number:6 6 and 6 are equal
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
Note: Join free Sanfoundry classes at Telegram or Youtube
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.