Home / Programs / Write a Java program to print every integer between 1 and n divisible by m. Also report whether the number that is divisible by m is even or odd.
Programming Example

Write a Java program to print every integer between 1 and n divisible by m. Also report whether the number that is divisible by m is even or odd.

👁 15 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:

Enter value of n: 20
Enter value of m: 4

Expected Output:

Numbers between 1 and 20 divisible by 4:
4 is Even
8 is Even
12 is Even
16 is Even
20 is Even

Program Code

import java.util.Scanner;

public class DivisibleCheck {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter value of n: ");
        int n = sc.nextInt();

        System.out.print("Enter value of m: ");
        int m = sc.nextInt();

        System.out.println("Numbers between 1 and " + n + " divisible by " + m + ":");

        for (int i = 1; i <= n; i++) {
            if (i % m == 0) {
                if (i % 2 == 0) {
                    System.out.println(i + " is Even");
                } else {
                    System.out.println(i + " is Odd");
                }
            }
        }

        sc.close();
    }
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.