Create a program that will ask the user to input a starting number and ending number. Based from the given range, display all odds, even and prime numbers
Sun Dec 08 2024 11:42:11 GMT+0000 (Coordinated Universal Time)
Saved by
@John_Almer
package oddevenprime;
import java.util.Scanner;
public class OddEvenPrime {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter starting number: ");
int start = input.nextInt();
System.out.print("Enter ending number: ");
int end = input.nextInt();
for (int num = start; num <= end; num++) {
if (num % 2 == 0) {
System.out.println(num + " is even");
} else {
System.out.println(num + " is odd");
}
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime && num > 1) {
System.out.println(num + " is prime");
}
}
}
}
content_copyCOPY
Comments