icse-promo

Question

A prime palindrome integer is a positive integer (without leading zeroes) which is prime as well as a palindrome. Given two positive integers m and n, where m < n, write a program to determine how many prime palindrome integers are there in the range between m and n (both inclusive) and output them.

The input contains two positive integers m and n where m < 3000 and n < 3000. Display the number of prime palindrome integers in the specified range along with their values in the format specified below:

Test your program with the sample data and some random data:

 

				
					Example 1:
INPUT:
m = 100
n = 1000
OUTPUT:
THE PRIME PALINDROME INTEGERS ARE:
101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 919, 929
FREQUENCY OF PRIME PALINDROME INTEGERS: 15
Example 2:
INPUT:
m = 100
n = 5000
OUTPUT:
OUT OF RANGE.
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class PrimePalindromeNumber
{
    public static boolean isPrime(int n)
    {
        int c = 0,i=0;
        for(i = 1; i <= n; i++)
        {
            if(n % i == 0)
            {
                c++;
            }
            
        }
        if(c > 2)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    public static boolean isPalindrome(int n)
    {
        int rev = 0,temp=0;
        temp=n;
        while(temp>0)
        {
            rev = rev * 10 + temp % 10;
            temp=temp/10;
        }
        if(rev==n)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public static void main(String args[])
    {
        int i=0,m=0,n=0,count=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("m = ");
        m =sc.nextInt();
        System.out.print("n = ");
        n = sc.nextInt();
        if(m > 3000 || n > 3000 || m > n)
        {
            System.out.println("OUT OF RANGE.");
            return;
        }
        System.out.println("THE PRIME PALINDROME INTEGERS ARE:");
        for(i = m; i <= n; i++)
        {
            if(isPalindrome(i) && isPrime(i))
            {
                if(count == 0)
                    System.out.print(i);
                else
                {
                    System.out.print(", " + i);
                }
                count++;
            }
        }
        System.out.println();
        System.out.println("FREQUENCY OF PRIME PALINDROME INTEGERS: " + count);
    }
}    

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *