Using Java:
Write a program to prompt the user to input a positive integer and determine whether the input is a prime number. If the number is a prime number, then the program also outputs whether the number is a palindrome. For example, the numbers 5, 121, 3443, and 123454321 are palindromes.
Guide On Rating System
Vote
import java.util.Scanner;
public class PrimePalindrome {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int number = scanner.nextInt();
scanner.close();
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
if (isPalindrome(number))
System.out.println(number + " is also a palindrome.");
else
System.out.println(number + " is not a palindrome.");
} else {
System.out.println(number + " is not a prime number.");
}
}
// Function to check if a number is prime
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
// Function to check if a number is palindrome
public static boolean isPalindrome(int number) {
int reversedNumber = 0;
int originalNumber = number;
while (number != 0) {
int remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}
return originalNumber == reversedNumber;
}
}