Enter value of n: 20 Enter value of m: 4
Numbers between 1 and 20 divisible by 4: 4 is Even 8 is Even 12 is Even 16 is Even 20 is Even
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();
}
}
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.
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.