0% found this document useful (0 votes)
23 views2 pages

Java Program: Odd, Even, Prime Numbers

The Java program defines an array of numbers from 1 to 20 and allows the user to choose to display either odd, even, or prime numbers from that array. It uses a switch statement to handle user input and prints the corresponding numbers based on the selected option. If the user input is invalid, it outputs an error message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Java Program: Odd, Even, Prime Numbers

The Java program defines an array of numbers from 1 to 20 and allows the user to choose to display either odd, even, or prime numbers from that array. It uses a switch statement to handle user input and prints the corresponding numbers based on the selected option. If the user input is invalid, it outputs an error message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package arry;

import java.util.Scanner;

public class jamaay {

public static void main(String[] args) {


int[] numbs = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20};

for (int i = 0; i < numbs.length; i++) {


System.out.print(" " + numbs[i]);}

Scanner user_input = new Scanner(System.in);

System.out.println("\n\nOPTION");
System.out.println("A = Odd \nB = Even\nC = Prime");

System.out.print("\n\nPlease choose one from the given options: ");


String user_ip = user_input.nextLine();

boolean ask = true;

switch(user_ip) {
case "A":
System.out.println("Odd Numbers:");
for(int i=0; i < 20; i++){
if(numbs[i] % 2 != 0){
System.out.print(" " + numbs[i]);
}
}
break;

case "B":
System.out.println("Even Numbers:");
for(int i = 0; i < 20; i++){
if(numbs[i] % 2 == 0){
System.out.print(" " + numbs[i] );
}
}
break;

case "C":
System.out.println("Prime Numbers:");
for (int i = 2; i <= 20; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(" " + i);
}
}

break;

default:
System.out.println("Invalid Input");
}
}
}

You might also like