[Question 1 (Prime Palindrome)] ISC 2012 Practical [Solved]
Download the complete ISC 2012 Computer Science Practical Question Paper from here: [ISC 2012] Computer Science Practical Question Paper Question 1 A prime...
Download the complete ISC 2012 Computer Science Practical Question Paper from here: [ISC 2012] Computer Science Practical Question Paper
Question 1
A prime palindrome integer is a positive integer (without leading zeros) 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, 797, 919, 929
FREQUENCY OF PRIME PALINDROME INTEGERS : 15
Example 2
INPUT:
m = 100
n = 5000
OUTPUT: OUT OF RANGE
Solution:
/**
* The class PalPrime prints all the Prime Palindrome numbers in the given range
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC 2012 Question 1
*/
import java.util.Scanner;
class PalPrime
{
Scanner sc = new Scanner(System.in);
/* Function isPrime( ) returns 'true' when the number 'x' is Prime and 'false' if it is not. */
boolean isPrime(int x)
{
int count=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}
/*Function isPalin( ) returns 'true' when 'x' is a Palindrome and 'false' if it is not.*/
boolean isPalin(int x)
{
int rev=0,dig;
int copy=x;
while(x>0)
{
dig=x%10;
rev=rev*10+dig;
x=x/10;
}
if(rev==copy)
return true;
else
return false;
}
/* Function showPalPrime( ) accepts the lower and upper limit, and prints all the PalPrime numbers
in between that range by sending each numbers in the range to both the functions isPrime( ) and isPalin( ) */
public void showPalPrime()
{
int m,n;
int c=0;
System.out.print("Enter the Lower Limit (m) = ");
m=sc.nextInt();
System.out.print("Enter the Upper Limit (n) = ");
n=sc.nextInt();
if(m>=n || m>=3000 || n>=3000) // Checking the range of Limits as given in the question
System.out.println("OUT OF RANGE");
else
{
System.out.println("The Prime Palindrome integers are:");
/* The below for loop generates every number starting from 'm' till 'n' and sends it
to both functions isPalin() and isPrime(), to check whether they are both Palindrome and
prime or not. If yes, then they are printed. */
for(int i=m; i<=n; i++)
{
if(isPrime(i)==true && isPalin(i)==true)
{
if(c==0)
System.out.print(i);
/*The above line is printing the first PalPrime number in order to maintain the sequence
of giving a comma ',' before every next PalPrime number, as is given in the Sample Output.*/
else
System.out.print(", "+i);
c++; //Counting the number of PalPrime numbers by incrementing the counter
}
}
System.out.println("Frequency of Prime Palindrome integers: "+c);
}
}
/* The main method creates an object of PalPrime Class and calls the function showPalPrime( ) */
public static void main(String args[])
{
PalPrime ob=new PalPrime();
ob.showPalPrime();
}
}Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.