Stuck with a problem

I am stuck with a problem must complete by tomorrow, most is completed but the output is not correct. This is a link to the question:
http://pages.ramapo.edu/~sfrees/courses/cmps148/HW/HW1.pdf

here is my code, if you see anything I am doing wrong, please help me out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#include <iostream>
#include <cmath>
using namespace std;



bool isPrime (int value){
    //1 is not prime
    if(value <= 1){
        return false;
    }
    //2 is prime
    if (value == 2) {
        return true;
    }
    //test to see if is prime
    for(int i = 2; i <= sqrt(value); i++){
        if (value % i ==0) {
            return false;
        }
    }
    return true;
}


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;
    
}
Last edited on
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.
Last edited on
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.
Topic archived. No new replies allowed.