(Emirp) An emirp (prime spelled backward) 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 both emirps. Write a Java program that displays the first 100 emirps. Display 10 numbers per line with a spacing of 5; all numbers should be left-aligned.

Question
Asked Oct 3, 2019
232 views

(Emirp) An emirp (prime spelled backward) 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 both emirps. Write a Java program that displays the first 100 emirps. Display 10 numbers per line with a spacing of 5; all numbers should be left-aligned. 

check_circle

Expert Answer

Step 1

The Java program is written to display first 100 emrip numbers which are nonpalindromic numbers whose reverse is also prime number. The numbers are displayed with spacing of 5 and left alignment. 10 numbers are displayed per line.

Step 2

public class Emirp

{

 

    public static void main(String[] args) {

 

        int cnt = 0;

        for (int i = 10; cnt < 100; i++) {

 

            if (primeNum(i) && primeNum(rev(i)) && !palindromeNum(i))

            {

                System.out.printf("%-10d", i); // left alignment with spacing of 5

                cnt++;

                if (cnt % 10 == 0 && i != 0) System.out.println();

 

            }

 

        }

    }

 

    public static boolean primeNum(long n) {

 

        if (n < 2) return false;

 

        for (int i = 2; i <= n / 2; i++) {

 

            if (n % i == 0) return false;

        }

 

        return true;

    }

 

    public static long rev(long number) {

 

        long rev = 0;

        while (number != 0) {

 

            rev *= 10;

            rev += number % 10;

            number /= 10;

        }

 

        return rev;

    }

 

    public static boolean palindromeNum(long number) {

 

        return (number == rev(number));

    }

}

Step 3

In the above program, three static methods are created primeNum () and palindromeNum () both in Boolean type and rev of type long. In primeNum me...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Object Oriented Programming

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: What is the difference between a trap and an interrupt?

A: DefinitionTrap is prompted by a user program to invoke Operating System functionality.Interrupt is t...

question_answer

Q: Hello, please I need help with the following using Python:   Write flowchart, pseudo-code and progra...

A: Pseudocode:Import module statistics and Declare variables Students and Student_listPrompt for the nu...

question_answer

Q: write a program in c++. You work for a box company, and have been asked to write a program to compar...

A: Solution:The C++ program has the following algorithm:Declare and initialize the required variables.D...

question_answer

Q: In the discussion of ARQ protocol in Section 3.3.3, a scenario was outlined that resulted in the rec...

A: “Yes”, it is possible to allow the receiver to receive multiple copies of the same frame when the me...

question_answer

Q: Fill in the blanks in the scheduling table. One schedule has been started for you. Each column repre...

A: FIFO (First in first out) scheduling algorithm is a non-preemptive algorithm which executes the proc...

question_answer

Q: I need help answering the following quetions I am not good with code. 1. Describe how you use TCP Du...

A: Use the keyword tcpdump with the TCP Dump flag –D.The flag –D will list all the interfaces that are ...

question_answer

Q: Code should be in C++ Absolute Value Template: Write a function template that accepts an argument an...

A: Since Function Templates are used to handle all possible data types you can say generic data type at...

question_answer

Q: Weighted Intervals Problem EXAMPLE:Suppose we have the following weighted intervals. start   finish ...

A: Instructions for solving the problem.Sort the given intervals by finish time, and the intervals with...

question_answer

Q: How do I solve this problem that deals with counting and subtracting calories in Java without using ...

A: Create a new class with name FitnessPlanner1.java, then take the inputs from user.Calculate the VO2,...