Wednesday, February 26, 2020

Prime-Adam Numbers Checking Program Using BlueJ


A Prime-Adam integer is a positive integer (without leading zeros) which is prime as well as an Adam number.


Prime number: A number which has only two factors, i,e 1 and the number itself.
Example: 2,3,5,7 etc

Adam number: The square of a number and the square of it's reverse are reverse to each other.

Example: If n=13 and reverse of 'n'=31 then square of 13 = 169 and square of 31 is 961 which is reverse of 169.

Thus 13 is an Adam number

import java.io.*;
class PrimeAdam
{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  void show() throws Exception
 {
      int m,n,i,f=0;
      System.out.print("\nEnter value of 'm': ");
      m=Integer.parseInt(br.readLine());
      System.out.print("\nEnter value of 'n': ");
      n=Integer.parseInt(br.readLine());
      if(m>=n)
      {
          System.out.print("\nINVALID INPUT");
          return;
        }
        System.out.print("\nTHE PRIME-ADAM INTEGERS ARE\n");
      for(i=m;i<=n;i++)
      {         
          if(prime(i) && adam(i))
          {
          System.out.print(" "+i);
          f++;
        }
      }
      if(f==0)
      System.out.print("\nNIL");
      else
      System.out.print("\nFREQUENCY OF PRIME-ADAM INTEGERS IS: "+f);
    }
   private boolean prime(int n)
    {
        int i;
           for(i=2;i<=n-1;i++)
           {
               if(n%i==0)
               break;
            }
            if(i==n)
            return true;
            else
            return false;
    }
     private boolean adam(int n)
   {
       int x,y;
       x=n*n;
       y=rev(n);
       y=y*y;
       y=rev(y);
       if(x==y)
       return true;
       else
       return false;
    }

  private  int rev(int n)
    {
        int i,r=0;
        for(i=n;i> 0;i=i/10)
        {
             r=r*10+i%10;
         }
         return r;
     }
     public static void main(String args[]) throws Exception
     {
         PrimeAdam ob=new PrimeAdam();
         ob.show();
      }
    }


Variable Description


Type
Variable
Purpose
Scope
BufferedReader
br
Intake values from user
Used within void show () function
int
m
Stores the lower range
Used within void show () function
int
n
Stores the upper range
Used within void show () function
int
i
Loop control variable
Entire program
int
f
Counts the Prime Adam numbers
Used within void show () function


Algorithm

Step 1: Create BufferedReader class object br
Step 2: Create int type variables m,n,i and f with 0 as initial value at f
Step 3: Take lower and upper range from user and store in m and n
Step 4: Generate the numbers between lower to upper range using a loop and check whether the number is Prime and Adam number using two different user defined functions.
Step 5: If the number is both Prime and Adam number, display it and increase the counter ‘f’ by 1
Step 6: At the end of loop, display the counter ‘f’

Step 7: End

BACK TO 2020 ISC COMPUTER PRACTICAL PAPER: CLICK HERE
     

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner