Palprime Number in Java

In this blog, we will learn about Palprime Number in Java, their importance, and how to work with them using Java programming.

What are palprime Number in Java?

A palprime number is a singular numerical object that combines the characteristics of prime numbers and palindromic numbers in Java. It is a number that is simultaneously a palindrome and a prime number. 

In Java, a palprime number refers to a unique combination of a palindrome and a prime number. It is an integer that is both a palindrome (reads the same forwards and backward) and a prime number (divisible only by 1 and itself).

Examples of Palprime Number in Java

Let’s explore a few examples of palprime numbers for a better understanding of concept:

  1. 11: This is the smallest palprime number. It is a palindrome as it reads the same forwards and backward, and it is also a prime number.
  1. 131: Another example of a palprime number. It is a palindrome and a prime number.
  1. 373: This is a larger palprime number. It is symmetric, meaning it reads the same forwards and backward, and it is a prime number.
  1. 929: Yet another palprime number. It is both a palindrome and a prime number.
  2. 151: This number is a palprime. It is symmetrical and a prime number.

Checks whether a given number is a PalPrime number or not 

//import required classes and packages  
import java.io.*;  
import java.util.*;  
  
//create PalPrimeNumber class to check whether the given number is a PalPrime number or not  
class PalPrimeNumEx  
{     
    //main() method start  
    public static void main(String args[])  
    {  
        //create scanner class object to get input from user  
        Scanner sc = new Scanner(System.in);  
          
        //declare and initialize variables   
        int num, temp, rem, i;  
        int count = 0;  
        int sum = 0;  
          
        System.out.println("Enter number to check");  
        //get input from user  
        num = sc.nextInt();  
          
        //store number in a temporary variable temp  
        temp = num;  
          
        //use for loop check whether number is prime or not  
        for(i = 1; i <= temp; i++)  
        {  
            if(temp%i == 0)  
            {  
                count++;    //increment counter when the reminder is 0  
            }  
        }  
          
        //use while loop to check whether the number is palindrome or not  
        while(num > 0)  
        {  
            // get last digit of the number  
            rem = num%10;  
              
            // store the digit last digit  
            sum = sum*10+rem;   
              
            // extract all digit except the last  
            num = num/10;   
        }  
          
        //check whether the number is palindrome and Prime or not  
        if(temp == sum && count == 2)  
        {  
            System.out.println(temp +" is a PalPrime number");  
        }  
        else  
        {  
            System.out.println(temp +" is not a PalPrime number");  
        }  
    }  
}

Explanation of the code:
It prompts the user to enter a number and then performs two checks. First, it checks whether the number is a prime number by using a for loop to iterate from 1 to the number itself and counting the number of factors. If the count is exactly 2 (divisible only by 1 and itself), the number is considered prime. Second, it checks whether the number is a palindrome by using a while loop to reverse the number and store it in a separate variable. If the reversed number is equal to the original number, it is considered a palindrome. Finally, it determines if the number satisfies both conditions of being a prime number and a palindrome, labeling it as a PalPrime number, or else it is labeled as not a PalPrime number.

Know the Prime Number Program in Java here!

Output:

Enter number to check
353
353 is a PalPrime number

Find all the PalPrime number within a given range 

import java.io.*;  
import java.util.*;  
  
//create FindAllPalPrimeNumber class to get all the ORE number in the given range  
class FindPalPrimeNumEx  
{     
    //main() method start  
    public static void main(String args[])  
    {  
        int sRange, eRange;  
          
        //create scanner class object  
        Scanner sc=new Scanner(System.in);  
          
        //show custom message  
        System.out.println("Enter start value");  
          
        //store user entered value into variable startRange  
        sRange = sc.nextInt();  
          
        //show custom message  
        System.out.println("Enter last value");  
          
        //store user entered value into variable endRange  
        eRange = sc.nextInt();  
          
        System.out.println("The PalPrime Numbers between" + sRange + " and " + eRange + "are:");  
        for(int i = sRange; i <= eRange; i++){  
            if(checkPalPrimeNumber(i))  
                System.out.println(i);  
        }  
          
    }  
      
    static boolean checkPalPrimeNumber( int number){  
          
        //declare and initialize variables   
        int temp, rem, i;  
        int count = 0;  
        int sum = 0;  
          
        //store number in a temporary variable temp  
        temp = number;  
          
        //use for loop check whether number is prime or not  
        for(i = 1; i <= temp; i++)  
        {  
            if(temp%i == 0)  
            {  
                count++;    //increment counter when the reminder is 0  
            }  
        }  
          
        //use while loop to check whether the number is palindrome or not  
        while(number > 0)  
        {  
            // get last digit of the number  
            rem = number%10;  
              
            // store the digit last digit  
            sum = sum*10+rem;   
              
            // extract all digit except the last  
            number = number/10;   
        }  
          
        //check whether the number is palindrome and Prime or not  
        if(temp == sum && count == 2)  
            return true;  
        else  
            return false;  
    }  
} 

Explanation of the code:

It prompts the user to enter a start value and an end value and then iterates through each number within that range. For each number, it calls the checkPalPrimeNumber() method to determine if it is both a prime number and a palindrome. The method performs two checks similar to the previous code: it checks if the number is prime by counting its factors, and it checks if the number is a palindrome by reversing it. If the number satisfies both conditions, it is considered a PalPrime number and is displayed as output. The code efficiently utilizes loops and conditional statements to identify and display the PalPrime numbers within the specified range.

Check out our blog on Palindrome Program in Java here!

Output:

Enter start value
500
Enter last value
1000
The PalPrime Numbers between 500 and 1000 are:
727
757
787
797
919
929

In conclusion, Palprime numbers in Java combine the unique properties of prime numbers and palindromic numbers. They are integers that are both prime and palindromic, making them intriguing mathematical objects. By understanding and working with Palprime numbers, Java programmers can enhance their problem-solving skills and explore the fascinating world of number theory. 

We hope you found our blog on “PalPrime Number in Java” informative and helpful. So, Keep exploring and expanding your knowledge to unlock the full potential of Java programming by visiting our website Newtum and keep learning coding programs with us!

About The Author

Leave a Reply