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 Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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();
    }
}

                        
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.