In the previous article, we have seen Java Program to Check Insolite Number
In this article we are going to seeĀ how we can write a program to find out whether the number is pointer prime or not.
Java Program to Check Pointer Prime Number
The number which is a prime number, and the next prime number can be find out by adding the product of the digit to its number is called pointer prime.
Let’s see different ways to check pointer prime number.
Method-1: Java Program to Check Pointer Prime Number
Approach :
- Initialize an integer variable and declare the value to it .
- Check the number is prime or not.
- If the number is prime, then find out the product of the digit and add to itself and check whether the result is next prime to it or not .
- If number is not prime the return false .
Program :
import java.util.*; public class Main { // Driver Code public static void main(String[] args) { // Given Number num int num = 1123; // Function Call if (isPointerPrime(num)) System.out.print("Entered number "+num+" is Pointer Prime"); else System.out.print("Entered number "+num+" is not Pointer Prime"); } // Function that returns true if a is prime else returns false static boolean isPrime(int num) { // Corner cases if (num <= 1) return false; if (num <= 3) return true; // This is checked so that we can skip middle five numbers in below loop if (num % 2 == 0 || num % 3 == 0) return false; for (int x = 5; x * x <= num; x = x + 6) if (num % x == 0 || num % (x + 2) == 0) return false; return true; } // Function to find the product of digits of num number N static int digprod(int num) { int prod = 1; while (num != 0) { prod = prod * (num % 10); num = num / 10; } return prod; } // Function to return the next prime static int nxtprm(int num) { // Base case if (num <= 1) return 2; int prime = num; boolean found = false; // Loop continuously until isPrime returns true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Function to check Pointer-Prime numbers static boolean isPointerPrime(int num) { if (isPrime(num) && (num + digprod(num) == nxtprm(num))) return true; else return false; } }
Output: Entered number 1123 is Pointer Prime
Method-2: Java Program to Check Pointer Prime Number
Approach :
- Initialize a integer variable and take the value for it as user input.
- Check the number is prime or not
- If the number is prime then find out the product of the digit and add to itself and check whether the result is next prime to it or not .
- If number is not prime the return false .
Program :
import java.util.*; public class Main { // Driver Code public static void main(String[] args) { Scanner s = new Scanner(System.in); // entering the number through user input System.out.print("Enter a number : "); int num= s.nextInt(); // Function Call if (isPointerPrime(num)) System.out.print("Entered number "+num+" is Pointer Prime"); else System.out.print("Entered number "+num+" is not Pointer Prime"); } // Function that returns true if a is prime else returns false static boolean isPrime(int num) { // Corner cases if (num <= 1) return false; if (num <= 3) return true; // This is checked so that we can skip middle five numbers in below loop if (num % 2 == 0 || num % 3 == 0) return false; for (int x = 5; x * x <= num; x = x + 6) if (num % x == 0 || num % (x + 2) == 0) return false; return true; } // Function to find the product of digits of num number N static int digprod(int num) { int prod = 1; while (num != 0) { prod = prod * (num % 10); num = num / 10; } return prod; } // Function to return the next prime static int nxtprm(int num) { // Base case if (num <= 1) return 2; int prime = num; boolean found = false; // Loop continuously until isPrime returns true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Function to check Pointer-Prime numbers static boolean isPointerPrime(int num) { if (isPrime(num) && (num + digprod(num) == nxtprm(num))) return true; else return false; } }
Output: Enter a number : 23 Entered number 23 is Pointer Prime
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Related Java Programs: