icse-promo

Question

A Prime-Adam integer is a positive integer (without leading zeroes) 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ā€™ is 31, then, 132 = 169, and 312 = 961 which is reverse of 169. Thus, 13 is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:

				
					Test your program with the following data and some random data:
Example 1:
INPUT:
m = 5
n = 100
OUTPUT:
The Prime-Adam integers are:
11, 13, 31
Frequency of Prime-Adam integers is: 3
Example 2:
INPUT:
m = 100
n = 200
OUTPUT:
The Prime-Adam integers are:
101, 103, 113
Frequency of Prime-Adam integers is: 3
Example 3:
INPUT:
m = 50
n = 70
OUTPUT:
The Prime-Adam integers are:
NIL
Frequency of Prime-Adam integers is: 0
Example 4:
INPUT:
m = 700
n = 450
OUTPUT:
Invalid Input.
				
			

Share code with your friends

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

Code

				
					import java.util.Scanner;
public class PrimeAdam
{
    public static boolean isPrime(int n)
    {
        int frequencyOfFactors = 0;
        
        for(int i = 1; i <= n; i++)
        {
            if(n % i == 0)
            {
                frequencyOfFactors++;
            }
        }
        if(frequencyOfFactors==2)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }

    public static int reverse(int n)
    {
        int rev = 0;
        while(n>0)
        {
            rev=rev*10+(n%10);
            n=n/10;
        }
       
        return rev;
    }
    
    public static void main(String args[])
    {
        int m=0,n=0,count=0,i=0,rev=0,s1=0,s2=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("m = ");
        m = sc.nextInt();
        System.out.print("n = ");
        n = sc.nextInt();
        if(m >= n)
        {
            System.out.println("Invalid input.");
            
        }
        else
        {
            count = 0;
            System.out.println("The Prime-Adam integers are:");
            for(i = m; i <= n; i++)
            {
                if(isPrime(i))
                {
                    rev = reverse(i);
                    s1 = i * i;
                    s2 = rev * rev;
                    if(reverse(s1) == s2)
                    {
                        if(count == 0)
                        {
                            System.out.print(i);
                        }
                        else
                        {
                            System.out.print(", " + i);
                        }
                        count++;
                    }
                }
            }
            if(count == 0)
            {
                System.out.println("NIL");
            }
            else
            {
                System.out.println();
                System.out.println("Frequency of Prime-Adam integers is: " + count);
            }    
        }
    }
}

				
			

Coding Store

Leave a Reply

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