Thursday , 30 March 2017
Home » Class 12 » [Question 1 (Prime Palindrome)] ISC 2012 Practical [Solved]

[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 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.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC 2012 Question 1
*/

import java.io.*;
class PalPrime
{
BufferedReader br=new BufferedReader(new InputStreamReader(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() throws IOException
{
int m,n;
int c=0;
System.out.print("Enter the Lower Limit (m) = ");
m=Integer.parseInt(br.readLine());
System.out.print("Enter the Upper Limit (n) = ");
n=Integer.parseInt(br.readLine());

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[])throws IOException
{
PalPrime ob=new PalPrime();
ob.showPalPrime();
}
}

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

13 comments

  1. Sir,
    How to convert an integer to string in scanner class ?
    In bufferedreader we use Integer.to string(n);
    But what for scanner?
    Thank you

  2. Can you please make it simpler by BufferedReader?

  3. Sir,Your method of prime check is really tedious and has massive time and space complexities ,how would you consider the the following snippet……
    public static boolean prime_Check(int n)
    {
    int flag=0;

    for(int i=2;i<=n/2;i++)
    {
    if(n%i==0)
    {
    flag==1;

    break;
    }
    }

    if(flag==1)

    return false;

    else

    return true;
    }

    • Hello BIprodeep Roy,
      The code you gave is cool. We don’t use it just because it gives the number ‘1’ or any negative numbers as ‘PRIME’.
      We don’t want students to apply a check specially for ‘1’, hence we use the alternate code, which also suffices quite well for composite number by just changing the last if condition to ‘count>2’.

      Keeping complexity in mind, if additional check is added for numbers <= 1, then your code would be better. Thanks for sharing.

  4. भारतीय

    sir…. can u get me an easy method of programming, just for checking a prime no.

    • Here you go:

      import java.io.*;
      class Prime
      {
        public static void main(String args[])throwsIOException
        {
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           System.out.print(&quot;Enter any number : &quot;);
           int n = Integer.parseInt(br.readLine());
           int c = 0;
           for(int i = 1; i&lt;=n; i++)
           {
             if(n%i == 0)
                c++; // Counting number of factors of the given number
           }
           if(c == 2) // Prime number has only 2 factors '1' and itself
             System.out.println(&quot;The number is Prime&quot;);
           else
             System.out.println(&quot;The number is Not Prime&quot;);
        }
      }
      
  5. 2013 icse paper?

Leave a Reply

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