JAVA PLZ An emirp (prime spelled backwards) is a nonpalindromic prime number who
ID: 3700348 • Letter: J
Question
JAVA PLZ
An emirp (prime spelled backwards) is a nonpalindromic prime number whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so 17 and 71 are emirps. 131 is a prime, but it is also a palindromic prime, so it is not an emirp. Write a program that displays the first 100 emirps. Display 10 numbers per line, separated by exactly one space. Sample output:
Make use of the methods reverse and isPalindrome that you wrote in the previous assignment PalindromeInteger.java. See the hints in the coding template.
Load detault template... 1 import java.util.scanner; 3 public class Emirp 4 public static void main(String[] args) f Use a loop and an emirp counter ranging from 1 to 100 (When you have reached 100 emirps this program should stop producing emirps). Use a second counteri to generate potential emirps. Inside the body of the loop, call the methods ?SPrine(i), !isPalíndrome(1), and ?sPrime(reverse(i)) to determine if i is an emirp When i is an emirp, increment the counter and also output i followed by a space. Every 10th emirp i needs to be output with a subsequent newline character instead of a space 6 7 10 public static boolean isPrime(int num) { 14 15 16 17 18 19 20 21 for (int i 2; iExplanation / Answer
import java.util.Scanner;
public class Emirp{
public static void main(String[] args){
int c=0;
int i=10;
//to print Emirp numnbers
while(c<100){
if(!isPalindrome(i)){
if(isPrime(i)){
int rev = reverse(i);
if(isPrime(rev)){
System.out.print(i);
System.out.print(" ");
if((c+1)%10==0)
System.out.print(" ");
c++;
}
}
}i++;
}
}
//to check prime or not
public static boolean isPrime(int num){
for(int i=2;i<=num/2;i++){
if(num%i==0){
return false;
}
}
return true;
}
//to get reverse number
public static int reverse(int num){
int rev=0;
int r;
while(num!=0){
r=num%10;
rev=rev*10+r;
num=num/10;
}
return rev;
}
//to check number is palindrome or not
public static boolean isPalindrome(int num){
int temp = num;
int rev=0;
int r;
while(temp!=0){
r=temp%10;
rev=rev*10+r;
temp=temp/10;
}
if(num == rev){
return true;
}else{
return false;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.