Computer Science, asked by 62rehankhan29, 6 months ago



Using the switch statement, Write a menu driven program:

a) Accept a number and display whether it is a prime or a composite number.

b) Accept a word and display whether it is a palindrome word or not.

plzzz answer urgent plz plz
but do wrong plz plz plz
if right I will follow you​

Answers

Answered by Oreki
1

import java.util.Scanner;

public class Numbers {

static boolean isPalindrome(String word) {

return word.equals(new StringBuffer(word).reverse( ).toString( ));

}

static boolean isPrime(int number) {

for (int i = 2; i < number; i++)

if (number % 2 = = 0)

return false;

return true;

}

public static void main(String[ ] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter\n1. Palindrome or\n2. Prime");

switch (sc.nextByte( )) {

case 1:

System.out.print("Enter a word - ");

System.out.printf("It is%sa Palindrome.", isPalindrome(sc.next( )) ? " " : " not ");

break;

case 2:

System.out.print("Enter a number - ");

System.out.printf("It is%sa Prime.", isPrime(sc.nextInt( )) ? " " : " not ");

break;

default :

System.out.print("Wrong Choice!");

}

}

}

Similar questions