# Thread: Why Errors in Prime Number Loop

1. ## Why Errors in Prime Number Loop

Hi, I'm writing a program that finds a prime number less than 600 and tests to see if the reverse is also prime (emirp). If so it should output the number. However, the program will not run. Can you please tell me where I am going wrong?

Code:
```int reverseNumber(int sourcenum);

int main ()
{
int number, counter, sum;

while(counter<600)
{
if (isPrime(number))
{
sum = reverseNumber(number);
if (isPrime(sum))
{
cout << sum;
return (0);
}
}
counter++;
}
return (0);
}

Logical isPrime(int integer)
{
for (int factor = 2; factor<integer; factor++) {
if ((integer % factor) == 0)
return False;
}
return True;
}

int reverseNumber(int sourcenum)
{
int temp = sourcenum;
int sum = 0;
while (temp)
{
sum*=10;
sum += temp%10;
temp/=10;
}
return sum;
}```

2. Well what is/are the error(s) you are getting ?

Edit: You didn't initialize counter and number.

3. You have used logical??but you haven't defined what's logical?? This may help you :-

Code:
```#include <iostream.h>
enum logical
{
false,
true,
};
int reverse(int n)
{
int rev=0;
while(n>0)
{
rev=rev*10+n&#37;10;
n=n/10;
}
return rev;
}
logical isprime(int n)
{
int i;
for(i=2;i<=n-1;i++)
{
if(n%i==0)
return false;
}
return true;
}

int main(void)
{
clrscr();
int n;
cout<<"\n\nEnter The Value Of N :";
cin>>n;
for(int i=2;i<n;i++)
{
if(isprime(i) && isprime(reverse(i)))
cout<<i<<"  ";
}

return 0;
}```

4. Code:
```enum logical
{
false,
true,
};```
Whats wrong with a bool?

5. Code:
```int reverseNumber(int sourcenum);
logical isprime(int n);
int main ()
...```

6. Originally Posted by cyberfish
Whats wrong with a bool?
Nothing wrong! but since he has used logical. I've kept it logical!

7. Originally Posted by chottachatri
Nothing wrong! but since he has used logical. I've kept it logical!
That seems logical.

8. It's like writing:
Code:
```enum UnsignedInteger {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
};```

9. if you really like the "logical":
Code:
`typedef bool Logical;`
would do the same job.

10. Alright, I noticed that I haven't initialized counter, or number, but when I do the program doesn't function as I would like. Basically I believe that there is something wrong with the loop. I'll show the code I've fixed thus far:

Code:
```#include <iostream>
using namespace std;

enum Logical {False, True};

Logical isPrime(int integer);
int reverseNumber(int sourcenum);

int main ()
{
int number, counter=1, sum;

while(counter<600)
{
if (isPrime(number))
{
sum = reverseNumber(number);
if (isPrime(sum))
{
cout << sum;
return (0);
}
}
counter++;
}
return (0);
}

Logical isPrime(int integer)
{
for (int factor = 2; factor<integer; factor++) {
if ((integer % factor) == 0)
return False;
}
return True;
}

int reverseNumber(int sourcenum)
{
int temp = sourcenum;
int sum = 0;
while (temp)
{
sum*=10;
sum += temp%10;
temp/=10;
}
return sum;
}```

11. Firstly, you are still using an uninitialized variable number, where you probably meant to use counter (lose number altogether, also note that 1 is not a prime).

Then you have a return (0) in the middle of the code (terminates as soon as the first value is found).

Thirdly, is there any reason not to use built-in bool which has values true and false?

12. Thanks for all the help I managed to get it working correctly. However, instead of outputting all the numbers in a list. I want to display it in a 10x10 grid. Any help or suggestions? Here's the code one last time...

Code:
```#include <iostream>
using namespace std;

enum Logical {False, True};

Logical isPrime(int integer);
int reverseNumber(int sourcenum);

int main ()
{
int number, counter=10, sum;

while(counter<1742)
{
if (isPrime(counter))
{
sum = reverseNumber(counter);
if (isPrime(sum))
cout << counter << endl;
}
counter++;
}
return (0);
}

Logical isPrime(int integer)
{
for (int factor = 2; factor<integer; factor++) {
if ((integer % factor) == 0)
return False;
}
return True;
}

int reverseNumber(int sourcenum)
{
int temp = sourcenum;
int sum = 0;
while (temp)
{
sum*=10;
sum += temp%10;
temp/=10;
}
return sum;
}```

13. Originally Posted by cyberfish
It's like writing:
Code:
```enum UnsignedInteger {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
};```
Ok but i m comfortable with enum only.!

14. And could you elaborate on how using bools is harder than actually creating an enum to simulate bools ? That makes no sense man.

15. and could you elaborate how using bool is faster than enum?it depends on preference !man!
Also i can't use bool as it's a new data type in C++ and as you might be knowing that i use Turbo C++ 3.0 compiler which is fairly old which doesn't support this data type. So i have to compulsorily use enum only!
I hope i am clear