Prime Number or Not

Ok, this is my new code that I wrote and no matter what I try it always says that 45 "prime number and not a prime number". 45 is clearly not a prime number but when divided by 2 it will come off as a prime number. How can I control the loop to test the number first and THEN do an output?

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

int main()
{
	int x, y, pcount=0, npcount = 0;
	bool prime = true;
	cout << "What are the prime factors of a given number?!.\n";
	cout << "Please enter a number.\n";
	cin >> x;
	for (y = 2; y <= 3;y++)
	{
		bool prime = true;
		if (x%y == 0)
		{
			prime = false;
			npcount++;
		}
	if (prime)
	{
		pcount++;
	}
	if (pcount == npcount)
	{
		cout << "Number is Not Prime!\n";
	}
	if (pcount > npcount)
	{
		cout << "Number is Prime!\n";
	}
	if (pcount < npcount)
	{
		cout << "Number is Not Prime!\n";
	}
	}
	return 0;
}
I had some old code lying around from a old topic, i don't know if this could be a solution for your problem.

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
// Example program - finding if a number is prime (w/o sieve which would be best)
#include <iostream>

int main()
{
    int prime = 0;
    while(std::cout << "Please enter a number to check if it is prime(0 to exit): " && std::cin >> prime && prime)
    {
        std::cout << prime << " is ";
        if(prime == 2)
        {
            std::cout << "prime." << std::endl;
        }
        else if(prime > 2) //anything above 2(possible prime)
        {
            bool isPrime = prime & 1; //if its odd then isPrime is true else false
            for(int i = 3; isPrime && i * i <= prime; i += 2) //iterate until the sqrt
            {
                isPrime = prime % i; //if it's not divisible it's possibly a prime
            }
            
            std::cout << (isPrime ? "prime." : "not prime.") << std::endl;
        }
        else //anything below 2 (not prime)
        {
            std::cout << "not prime." << std::endl;
        }
    }
    return 0;
}
Last edited on
Hey thanks for the help! I was able to use your logic into my own version of the code to check for if the number is prime or not. This is my project completed.

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;

void factors(int x);	//My function is decleared before the main program. I used void because im not returning a number.
int main()
{
	int x;
	bool prime = false;	//initialsed prime to be false.
	cout << "Is this number a Prime Number?\n";
	cout<<"If not then what are the Prime Factors of that number?.\n";
	cout << "Please enter a number.\n";
	cin >> x;	//user inputs number to compute.
	if ((x == 2) || (x == 3) || (x == 5) || (x == 7))	//This ensures that my test numbers will come out as prime instead of not prime.
		{
			cout << "This is a Prime Number!\n";
			return 0;	//When a 2,3,5, or 7 is entered the program will end as they are prime.
		}
	if ((x % 2 == 0) || (x % 3 == 0) || (x % 5 == 0) || (x % 7 == 0))	//The number the user entered will be tested by 2,3,5, and 7 to see if it is prime or not.
	{
		cout << "The number" << " " << x << " " << "is factored by\n";
		factors(x); //My function created to pull out the factors of that number.
	}
	else cout << "This is a Prime Number!\n";
	return 0;
}

void factors(int x)
{
	bool prime = true;	//prime is set to true.
	for (int z = 2; z <= x / 2; z++)	//variable z is declared and set to continue the loop until it equals the users number which is divided by 2.
	{
		bool prime = true;	//prime is set to true still.
		for (int y = 2; y <= z / 2; y++)	//Z represents the "new" number that the user entered and will be divided to see if z is prime or not.
		{
			if (z%y == 0)	//If z equals that of zero then it will change the bool prime to equal false.
			{
				prime = false;	//prime is changed to false.
			}
		}
		if (prime)	//This statement is then checked and if z turned out to be prime then it will set off this if statement. 
		{
			if (x%z == 0)	//The original number that the user entered x will be divided by the changing numbers of z to see if they evenly go
				cout << " " << z << " " << endl;	//into x. If that is proven true then that prime number is one of the factors of the users number.
		}											//It is then displayed onto the screen.
	}
}
Topic archived. No new replies allowed.