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.
Java Programming Language Decision Making in java (Article) Decision Making in java (Program)
12
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:
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();
}
}
Output:
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
Numbers between 1 and 20 divisible by 4: 4 is Even 8 is Even 12 is Even 16 is Even 20 is Even
Program:
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(); } }
Output:
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.