Emirp program using functions

Okay im having trouble piecing together this lab. you can view the instructions for the lab here: http://appiversity.blob.core.windows.net/instructor-1-section-18-assignment-0/3d972ae4-6a4e-4dbb-ae07-1544e6fab002_Homework3.pdf

Heres what I have so far:

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
#include <iostream>
using namespace std;

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).


int main(){
	int value, count = 0;

	do{
		cout << "Please enter a positive number: ";
		cin >> value;
	} while (value < 0);

	if (isPrime(value) == 1){
		for (int test = 0; test < value; test++){
			//prime number reverse?
		}

	system("pause");
}

bool isPrime(int value){
	bool prime;
	if (value % 2 == 0 || value % 3 == 0 || value % 5 == 0 || value % 7 == 0){
		prime = false;//0
	}
	else{
		prime = true;//1
	}
	return prime;
	}

int reverse(int value){
		int rnum = 0;

		while (value) {
			rnum = rnum * 10 + (value == 10);
			value /= 10;
		}

		return rnum;
	}



any help is appreciated i need this done asapppp
Last edited on
According to your requirements the user enters the number of primes, i.e. the do while loop on line 11 is wrong.

What you basically have to do is:
- A loop that iterate as long as you haven't found enough primes
- Find a prime
- Create the counter part (e.g. 13 -> 31)
- Check whether the counter part is prime
- Loop until you reach the number of primes the user has entered

the algorithm for finding a prime is as follows:
- A loop that iterates until sqrt(value)
- If you find (value % i) != 0 -> break, no prime
If you find (value % i) != 0 -> break, no prime


value is no prime if (value % i) == 0
#include <iostream>
#include<iomanip>

using namespace std;

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).


int main()
{
int n, count = 0, i = 1, a = 1;


cout << "Please enter a positive number: ";
cin >> n;

cout<<endl<<endl;

while(count < n)
{
i++;

if(isPrime(i))
{
if(isPrime( ( reverse(i) ) ) )
{
cout<<"\t"<<i;
count++;
}
}

if( (count - (5*a)) == 0 )
{
cout<<"\n";
a++;
}
}
cout<<endl;
return 0;
}

bool isPrime( int value)
{
int test = 2, count = 0;

while(test < value)
{

if( value % test == 0)
count ++;
test++;
}
if(count == 0)
return 1;
else
return 0;
}
int reverse(int value)
{
int test = 2, rev = 0, counter;

while( value > 0)
{
rev = rev * 10 + (value % 10);
value = value / 10;
}
return rev;

}
Topic archived. No new replies allowed.