Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code must be done in a basic form of c++ and the best answer will go to the

ID: 638114 • Letter: T

Question

This code must be done in a basic form of c++ and the best answer will go to the solution that not only answers the question but explains the steps used to reach the solution as well!

Calculate the first N emirp (prime, spelled backwards) numbers, where N is a positive number that the user provides as input. An Emirp is a 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. Write a program that prints out the first N emirps, five on each line.

For example:

For this assignment, you are required to make use of 2 functions (which you must write).

bool isPrime(int value);          // Returns true if value is a prime number.

int reverse (int value); // Returns the reverse of the value (i.e. if value is 35, returns 53).

You should follow a top-down design, using these functions in conjunction with logic in main, to perform the computation of N emirps and print them out according to the screenshot above. The general outline for main would be as follows:

        Step 1:         Ask user for positive number (input validation)

        

Step 2:         Initialize a variable Test to 2

        

Step 3: While # emirps found is less than the input:

Call isPrime with Test, and call it again with reverse(Test). If both are prime, print and increment number of emirps found.

Test++

Explanation / Answer

// Online URL for testing : http://ideone.com/KQFiPO
#include <math.h>       // Header file Imported for SQRT function to get the square roots.
#include <iostream>       // Header file Imported for cin and cout operations

using namespace std;

bool isPrime (int num)
{
if (num <=1)               // number input less<=1 is not prime
return false;
else if (num == 2) // if number == 2 then prime
return true;
else if (num % 2 == 0)       // if number divisble by 2 then not prime... this is to avoid unnecessary loops run.
return false;
else
{
bool prime = true;
int divisor = 3;
double num_d = static_cast<double>(num);
int upperLimit = static_cast<int>(sqrt(num_d) +1);       // to hold square root of the number input
  
while (divisor <= upperLimit)                           // Loop to check the divisibility of odd numbers from 3 to sqrt of number entered. If yes anytime, then not prime
{
if (num % divisor == 0){
prime = false;
               break;
           }
divisor +=2;
}
return prime;                                           // Return true if not divisible by any numbers else false.
}
}

int reverse(int value){
int rev=0;                       //Holds the reverse
for( ; value!= 0 ; )               //Runs the loop till the final value after division to 10 is 0.
{
rev = rev * 10;               //Multiplies the last digit with 10 to reverse it.
rev = rev + value%10;           //Returns the last digit in each run and adds the nultiplied 10 value.
value = value/10;
}
return rev;                       // Returns the reversed number.
}

int main(){
   int iCounter=0,iMax;                                                    //iCounter stores the Nth number of series, iMax = maximum number to display.
   int iColCounter=1,iMaxColumn=5;                                           //iColCounter = nth column and iMaxColumn = Max column to display
   int iPrime=2;                                                           //iPrime stores the prime and gets incremented in loop and starts with 2
   while(true){                                                           //loop to get the user input until the entered number is greater than 0.
       cout<<"Enter the number of Nprimes to generate (+ve number)";
       cin>>iMax;
       if(iMax>0) break;                                                   //Breaks the loop if input is greater than 0.
       cout<<"The number entered should be +ve";                           //displays the error message
   }
   cout<<endl;
   while(iCounter<iMax){                                                   // Runs the loop till Nth Nprimes is iMax i.e. entered.
       while(!( (isPrime(iPrime)) && (isPrime(reverse(iPrime))))){           // isPrime(number) = returns if the number is prime, reverse(number) = returns the reverse of the number;
           iPrime++;                                                       // if the above loop is true i.e. iPrime is not prime then increment iPrime++
       }                                                                   // Loops check the new iPrime again.
       cout<<iPrime<<" ";                                                   // if the iPrime is prime prints it.
       if ((iColCounter++)==iMaxColumn){                                   // if MaxColumn reached to 5 then displays end of line.
           iColCounter=1;
           cout<<endl;
       }
       iCounter++;                                                           // iCounter increased for next Nth prime.
       iPrime++;                                                           // Goes to next number
   }
   return 0;                                                               // Exits the main method.
}

Output: