JAVA Program To Check Given Number Is Prime-Adam or Not [ISC 2020 Practical]

JAVA PROGRAM TO CHECK GIVEN NUMBER IS PRIME ADAM OR NOT

Introduction:

A Prime-Adam integer is a positive integer (without leading zeros) which is a 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 its reverse are reverse to each other.

Example:
If n=13 and reverse of "n" =31, then, 
(13)2 = 169 
(31)2 =961 which is reverse of 169
thus 13, is an Adam number.

Test your program with the following data .



CODE

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * QUESTION : A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as well as an Adam number.
 * @theschoolprogrammer
 * www.theschoolprogrammer.com 
 */

import java.util.*;
public  class PrimeAd
{ 
    static boolean Prime(int n)
    //function to check given number is prime or not
{ int i,f=0; for(i=1;i<=n;i++) { if(n%i==0) f++; } if(f==2) return(true); else return(false); } static int Reverse(int n)//returns reverse of a number { String s=Integer.toString(n);
        //converting number to string for easy algorithm
        int i;
        String ns="";
        for(i=0;i<s.length();i++)
        {
            ns=s.charAt(i)+ns;

        }

        return(Integer.valueOf(ns));

    }

    static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER FIRST NUMBER");
        int n=sc.nextInt();

        int k=(int)Math.pow(Reverse(n),2);

        if((Prime(n)==true) &&(Reverse(n*n)==k))
        {              
            System.out.print("YES IT IS!!");
        }
        else
            System.out.print("NOPE");
    }
}



 Found this useful? Share with your friends.
                                                                     Share

Comments

You are welcome to share your ideas with us in comments!!!

Full Screen Mode

Archive

Contact Form

Send