Java Program to Check Multiples of a Number

In this tutorial, we will learn how to write a Java program to check multiples of a number.

We will first understand the basic concept of multiples and the modulo operator, and then implement the solution using user input from the Scanner class with both an if-else statement and a ternary operator.

What Is a Multiple of a Number?

A multiple of a number is a value that can be divided evenly by that number without leaving a remainder. For example:

  • Multiples of 3 include 3, 6, 9, 12, 15, 18, and so on.
  • Multiples of 7 include 7, 14, 21, 28, 35, 42, and so on.

In Java, we can determine this using the modulus operator (%), which calculates the remainder of a division. For any given number:

  • number % 3 == 0 checks if it is a multiple of 3.
  • number % 7 == 0 checks if it is a multiple of 7.

For instance, 21 is a multiple of both 3 and 7 because both 21 % 3 and 21 % 7 evaluate to 0.

How to Check Multiples of a Number in Java

To determine whether a number is a multiple of another number, we use the modulus operator (%) with two variables: the number being checked and the divisor. The general condition is:

number % divisor == 0

If this condition evaluates to true, the number is a multiple of the divisor. If it evaluates to false, it is not.

Example:

  • If number = 50 and divisor = 5: 50 % 5 equals 0, so 50 is a multiple of 5.
  • If number = 50 and divisor = 7: 50 % 7 equals 1 (leaving a remainder), so 50 is not a multiple of 7.

Now, let us implement this logic in Java.

Java Program to Check Whether a Number Is a Multiple of 3

Problem Statement: Write a Java program to check whether a given integer is a multiple of 3. If the number is divisible by 3, display an appropriate message; otherwise, display that the number is not a multiple of 3.

// Java program to check whether a number is a multiple of 3.
public class MultipleOfThreeChecker {
    public static void main(String[] args) {
        // Define the integer to be checked
        int number = 15; // You can change this value to test other numbers

        // Check if the number is a multiple of 3 using the modulus operator
        if (number % 3 == 0) {
            // If the remainder is 0, the number is divisible by 3
            System.out.println(number + " is a multiple of 3.");
        } else {
            // If the remainder is not 0, the number is not divisible by 3
            System.out.println(number + " is not a multiple of 3.");
        }
    }
}

Output:

15 is a multiple of 3.

In this example program:

  • We declare an integer variable named number and assign it a value to be checked.
  • The modulus calculation (number % 3) divides the integer by 3 and computes the remainder of the division.
  • The condition evaluation (if (number % 3 == 0)) checks if the remainder is equal to zero, which mathematically defines whether a number is a multiple of 3.
  • If the condition is true, it outputs a message confirming that the number is a multiple.
  • If the condition is false, it outputs a message stating that the number is not a multiple.

Java Program to Check Multiples of a Number Using Scanner

Problem Statement: Write a Java program that accepts two integers from the user and checks whether the first number is a multiple of the second number. Display an appropriate message based on the result.

// Java program to check whether a number is a multiple of another number using Scanner class.
import java.util.Scanner;
public class CheckMultiple {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the console
        Scanner sc = new Scanner(System.in);

        // Prompt and read the first number (the number to check)
        System.out.print("Enter the first number: ");
        int num1 = sc.nextInt();

        // Prompt and read the second number (the divisor)
        System.out.print("Enter the second number: ");
        int num2 = sc.nextInt();

        // Check if the second number is non-zero to prevent division by zero,
        // and check if the first number is a multiple of the second using the modulus operator
        if (num2 != 0 && num1 % num2 == 0) {
            System.out.println(num1 + " is a multiple of " + num2 + ".");
        } else {
            System.out.println(num1 + " is not a multiple of " + num2 + ".");
        }

        // Close the scanner resource to free up system resources
        sc.close();
    }
}

Output (When it is a multiple):

Enter the first number: 20
Enter the second number: 4
20 is a multiple of 4.

Output (When it is not a multiple):

Enter the first number: 17
Enter the second number: 5
17 is not a multiple of 5.

In this example program:

  • The statement Scanner sc = new Scanner(System.in); creates a Scanner object that reads input from the user’s keyboard.
  • The statement int number = sc.nextInt(); reads the integer entered by the user and stores it in the number variable.
  • Next, the if-else statement checks whether the remainder after division is zero.
  • If the condition is satisfied, the program prints that the number 20 is a multiple of 4.
  • Otherwise, it prints that the number is not a multiple of either number.

This approach allows the user to test different integers without changing the program code.

Java Program to Check Whether a Number Is a Multiple of 3 or 7

Problem Statement: Write a Java program to check whether an integer number is a multiple of 3 or 7. If the number is divisible by either 3 or 7, we will display an appropriate message. Otherwise, we will display that the number is not a multiple of 3 or 7.

// Java program to check whether a given number is a multiple of 3 or 7
public class MultipleOfThreeOrSeven {
    public static void main(String[] args) {
        // Define the integer to be checked
        int number = 21; // You can change this value to test other numbers

        // Check if the number is a multiple of 3 OR 7 using the modulus operator and logical OR (||)
        if (number % 3 == 0 || number % 7 == 0) {
            // Executed if the number is divisible by 3, 7, or both
            System.out.println(number + " is a multiple of 3 or 7.");
        } else {
            // Executed if the number is not divisible by either 3 or 7
            System.out.println(number + " is not a multiple of 3 or 7.");
        }
    }
}

Output:

21 is a multiple of 3 or 7.

In this example program:

  • We declare an integer variable named number and assign it the value 21.
  • The condition number % 3 == 0 checks whether the number is divisible by 3 without a remainder.
  • The condition number % 7 == 0 checks whether the number is divisible by 7 without a remainder.
  • The logical OR operator (||) returns true if at least one of these conditions is true.
  • If the condition is true, the if block displays that the number is a multiple of 3 or 7. Otherwise, the else block displays the opposite message.

Since 21 is divisible by both 3 and 7, the condition evaluates to true, and the program prints the expected output.

Use of Ternary Operator

You can also write the ternary operator to check whether a number is a multiple of 3 or 7.

// Check whether the number is a multiple of 3 or 7
String result = (number % 3 == 0 || number % 7 == 0)
                ? "is a multiple of 3 or 7."
                : "is not a multiple of 3 or 7.";

The ternary operator evaluates the condition:

number % 3 == 0 || number % 7 == 0

If the condition is true, the expression returns the message “is a multiple of 3 or 7.”. If the condition is false, it returns the message “is not a multiple of 3 or 7.”. The resulting message is stored in the result variable and displayed using System.out.println().

For beginners, the if-else approach is generally easier to understand. The ternary operator is useful when you want to express a simple decision in a single expression.

Java Program to Check Whether a Number Is a Multiple of Both Numbers

Sometimes, the requirement is to check whether a number is divisible by both numbers rather than either. For example, suppose we want to check if a number is divisible by both 3 and 7. In this case, we use the logical AND operator (&&) instead of the logical OR operator (||).

The condition is:

number % 3 == 0 && number % 7 == 0

Problem Statement: Write a Java program to check whether an integer number is a multiple of both 3 and 7. If the number is divisible by both 3 and 7, we will display an appropriate message. Otherwise, we will display that the number is not a multiple of 3 and 7.

// Java program to check whether a number is a multiple of both 3 and 7
public class MultipleOfBothThreeAndSeven {
    public static void main(String[] args) {
        // Declare and initialize an integer to be tested
        int number = 42; // You can change this value to test other numbers

        // Check if the number is a multiple of both 3 AND 7 using the modulus operator and logical AND (&&)
        if (number % 3 == 0 && number % 7 == 0) {
            // Executed if the number leaves a remainder of 0 when divided by both 3 and 7
            System.out.println(number + " is a multiple of both 3 and 7.");
        } else {
            // Executed if the condition fails for at least one of the numbers
            System.out.println(number + " is not a multiple of both 3 and 7.");
        }
    }
}

Output:

42 is a multiple of both 3 and 7.

In this example, the logical AND operator (&&) requires both conditions to be true. Since both conditions 42 % 3 == 0 and 42 % 7 == 0, evaluate to true, the program displays that 42 is a multiple of both 3 and 7.

Practice Challenge: Check Divisibility by 3, 7, or Both

Problem Statement: Write a Java program that accepts an integer from the user and checks its divisibility according to the following rules:

  1. If the number is a multiple of both 3 and 7, display an appropriate message.
  2. If the number is a multiple of 3 only, display an appropriate message.
  3. If the number is a multiple of 7 only, display an appropriate message.
  4. If it is a multiple of neither, display an appropriate message.
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.