Write a Java program to check whether the first number is a multiple of second number

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); // 👉️ 0

In 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); // 👉️ 2

You 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); // 👉️ -0

However, 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.

  1. import java.util.Scanner;
  2. public class Equal_Integer
  3. {
  4. public static void main(String[] args)
  5. {
  6. int m, n;
  7. Scanner s = new Scanner(System.in);
  8. System.out.print("Enter the first number:");
  9. m = s.nextInt();
  10. System.out.print("Enter the second number:");
  11. n = s.nextInt();
  12. if(m == n)
  13. {
  14. System.out.println(m+" and "+n+" are equal ");
  15. }
  16. else
  17. {
  18. System.out.println(m+" and "+n+" are not equal ");
  19. }
  20. }
  21. }

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

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

Postingan terbaru

LIHAT SEMUA