#include <iostream>
#include <cmath>
usingnamespace std;
bool isPrime (int value){
//1 is not prime
if(value <= 1){
returnfalse;
}
//2 is prime
if (value == 2) {
returntrue;
}
//test to see if is prime
for(int i = 2; i <= sqrt(value); i++){
if (value % i ==0) {
returnfalse;
}
}
returntrue;
}
int reverse(int value){
//initalize opposite number
int opp = 0;
// convert value to opp
while(value > 0){
opp = opp*10 + (value % 10);
value = value/10;
}
return opp;
}
int main() {
//variable dec
int n;
int test = 2;
int emirps_count = 1;
//ask user for number and validate
do {
cout << "Please enter a positive number: ";
cin >> n;
} while (!(n > 0));
//while loop to count emirps
while (emirps_count <= n) {
//call to start loop
isPrime(test);
isPrime(reverse(test));
if (isPrime(test)== 1 && isPrime(reverse(test))==1){
//print prime number and reverse of it
cout << test << endl;
cout << reverse(test) << endl;
//increase emirps
emirps_count++;
}
//increase test
test++;
}
return 0;
}
It looks like your functions work as advertised, but the loop in main that you use to exercise them is wrong. Lines 61 and 62 don't do anything. Comparing a boolean return value from a function to '1' is unnecessary. You're only supposed to output the satisfying prime number, not its reverse as well. You're not doing anything to line up the output and print 5 per line.
See if this works better:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
while (emirps_count < n)
{
if (isPrime(test) && isPrime(reverse(test)))
{
//print prime number, set the column width to 7 (or whatever you want)
std::cout << std::setw(7) << test;
//increase emirps
emirps_count++;
//if we're up to some multiple of 5 emirps, print a new line
if(emirps_count % 5 == 0)
{
std::cout << std::endl;
}
}
//increase test
test++;
}
EDIT: I should also note that I initialized emirps_count to 0 instead of 1. Update your line 47 accordingly.
okay, I guess I was thrown off by the way he addressed the steps. Thank you so much, just reading how you wrote it made it clear to me how it should be. I was working the problem thinking the output should be more complicated than it needed to be. Thank you again.