issue with a prime number program

Feb 25, 2016 at 6:27am
my program is having issues finding prime numbers.

this is my program when I input 1 it shows up as a prime number and some other problems may lay in here as well

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

int main()
{


int x, i, no; // x user imputed integer, i is remainder, no is a flag for the integer.
bool prime;
do{

cout << "Enter a positive integer(0 to exit program): ";
cin >> x;
if (x == 0){u

no = 0;
cout << "This is not a prime number"<<endl;

}
if (x != 1)
{
no = 1;
}
for(i != 2; i >= x/2; ++i)
prime = false;
{
if(x%i == 0)
{
no = 0;
}
for(i=2; i<=x/2; ++i)
prime = true;

}

if (prime = true)
cout << "This is a prime number"<<endl;

else
cout << "This is not a prime number"<<endl;

system ("pause");
system ("cls");
}
while (x > 0);


return 0;



}
Last edited on Feb 25, 2016 at 7:07am
Feb 25, 2016 at 7:37am
Hi,

Please always use code tags:

http://www.cplusplus.com/articles/z13hAqkS/


Do you Google this before you wrote the code? There are millions of others who have already done this. You could learn some ideas: Limit is sqrt not x/2; specifically test for num == 1 ; use for == for comparison, not =

Also look at the wiki page for this, near the end there is Euler's algorithm.

0 is supposed to exit the program, but it says it's not a prime. A good reason not to use a do loop, the condition is at the end :+)

The algorithm you are using is a typical naive one, and very inefficient - only good for small limits like 5,000 say. The problem is if the number is prime, it tests every multiple regardless of whether it is already a multiple of something else. If the number mod 2 is false then number mod 4, 6, 8 .... will be false too. The same story for all the other trial factors too.

It is much better to start off with a list of all the numbers, then cull out ones which are not prime, then search for a particular number in the remaining list. This is not as hard as it sounds.

Good Luck !!
Feb 25, 2016 at 12:00pm
On top of all the things TheIdeasMan said, you first for statement makes no sense:

for(i != 2; i >= x/2; ++i)

What is i != 2 in the initialization step supposed to be doing?
Last edited on Feb 25, 2016 at 12:00pm
Feb 25, 2016 at 4:52pm
i have improved on the code but 1 is not printing "1 is not prime" to the screen

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

int main()
{

	
  int x, c; // x user imputed integer, i is remainder, no is a flag for the integer.
bool prime = true;


  cout << "Enter a positive integer(0 to exit program): ";
  cin >> x;
  do {
  
  if (x == 0){
  	break;
  }
if (x < 2)
{
	continue;
}
else
{
	for (int c = 2; c <= sqrt (x); c++)
	{
		if (x % c == 0)
		{
		prime = false;
	}
}
if (prime == true){

cout << x << " Is Prime"<<endl;
}
else
{
	cout << x << " Is not prime"<<endl;
}
}
}
while (x = 0);
return 0;
}
Feb 25, 2016 at 5:13pm
TheIdeasMan wrote:
specifically test for num == 1


Edit:

TheIdeasMan wrote:
use for == for comparison, not =1
Line 44.

If you insist on using a do loop, put the while part with the closing brace:

} while (x == 0);

Otherwise it looks like a while loop with a null statement, not a do while loop.

You are missing a brace after line 31. If the code is indented properly you can see that straight away. Try and get your editor to auto-magically do the braces and indentation.

Apart from that it seems to work with some casual testing :+)
Last edited on Feb 25, 2016 at 5:24pm
Feb 25, 2016 at 5:42pm
You initialise prime to true. If x is 1, you don't do anything to set that flag to false.
Topic archived. No new replies allowed.