Computer Science
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.
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
Java
User Defined Methods
ICSE Prac 2020
66 Likes
Answer
import java.util.Scanner;
public class PrimeAdam
{
public static int reverse(int num) {
int rev = 0;
while (num != 0) {
int d = num % 10;
rev = rev * 10 + d;
num /= 10;
}
return rev;
}
public static boolean isAdam(int num) {
int sqNum = num * num;
int revNum = reverse(num);
int sqRevNum = revNum * revNum;
int rev = reverse(sqNum);
return rev == sqRevNum;
}
public static boolean isPrime(int num) {
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++;
}
}
return c == 2;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of m: ");
int m = in.nextInt();
System.out.print("Enter the value of n: ");
int n = in.nextInt();
int count = 0;
if (m >= n) {
System.out.println("INVALID INPUT");
return;
}
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
for (int i = m; i <= n; i++) {
boolean adam = isAdam(i);
if (adam) {
boolean prime = isPrime(i);
if (prime) {
System.out.print(i + " ");
count++;
}
}
}
if (count == 0) {
System.out.print("NIL");
}
System.out.println();
System.out.println("FREQUENCY OF PRIME-ADAM INTEGERS IS: " + count);
}
}Output





Answered By
24 Likes
Related Questions
Design a class to overload a function series( ) as follows:
(a) void series (int x, int n) – To display the sum of the series given below:
x1 + x2 + x3 + ………. xn terms
(b) void series (int p) – To display the following series:
0, 7, 26, 63 ………. p terms
(c) void series () – To display the sum of the series given below:
1/2 + 1/3 + 1/4 + ………. 1/10
Define a class to overload the method display() as follows:
void display(): To print the following format using nested loop.
1 2 1 2 1
1 2 1 2 1
1 2 1 2 1void display (int n, int m) : To print the quotient of the division of m and n if m is greater than n otherwise print the sum of twice n and thrice m.
double display (double a, double b, double c) — to print the value of z where
Consider the following program segment and answer the questions below:
class calculate { int a; double b; calculate() { a=0; b=0.0; } calculate(int x, double y) { a=x; b=y; } void sum() { System.out.println(a*b); }}Name the type of constructors used in the above program segment?
Invoking a method by passing the objects of a class is termed as:
- Call by reference
- Call by value
- Call by method
- Call by constructor