Prime Numbers

Not quite sure where my problem lies... I am still relatively new to programming. The code is supposed to determine if a number is prime or not. The problem is that, say for instance, the user enters 15. 15 is not prime but it will say that it is... the problem is the factor of 3 with another odd number... but I don't really see where to fix it... :|

As far as i can tell it works with everything except for the factor of 3 paired with another odd number... besides that it calls prime or not correctly...

any help is appreciated, a hint in the right direction would be better! Thanks for your time...

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
/*

	Write a program that prompts the user to input a positive integer. It should then
	output a message indicating whether the number is a prime number. (Note: an 
	even number is prime if it is 2. An odd integer is prime if it is not divisible by
	any odd integer less than or equal to the square root of the number.)	

*/

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
	double doubleNumber, doubleSquareRootOfNumber;
	int container = 0, counter = 1, number, squareRootOfNumber;
	bool flag = false, prime = false;

	cout << "Please enter a positive integer: "; 

	cin >> doubleNumber; 

	cout << endl;

	doubleSquareRootOfNumber = sqrt(doubleNumber); 

	number = (int)doubleNumber; 
	squareRootOfNumber = (int)doubleSquareRootOfNumber; 

	while(flag != true) 
	{
		if (number == 1 || number == 2)
		{
			cout << "The number " << number << " is a prime number..." << endl << endl;
			flag = true;
			break;
		}

		if (number > 2)
		{
			while(counter <= squareRootOfNumber)
			{
				counter++;
				container = number % counter;

				if(container == 0)
				{
					prime = false;
					flag = true;
					break;
				}
				else
				{
					prime = true;
					flag = true;
					break;
				}
			}

			if(prime == true)
			{
				cout << "The number " << number << " is a prime number..." << endl << endl;
			}

			if(prime == false)
			{
				cout << "The number " << number << " is not a prime number..." << endl << endl;
			}
		}
	}

	system("PAUSE");

	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
if(container == 0)
{
	prime = false;
	flag = true;
	break;
}
else
{
	prime = true;
	flag = true;
	break;
}


The "else" ensures that the parent while loop always breaks at the first iteration, not just while counter <= squareRootOfNumber evaluates to true. Is this intentional?
Last edited on
no, it wasn't... could that be why it won't work on factors of 3? I noticed that the sqrt of most small numbers dont make it past 4 so the counter does not get high enough to accurately determine the prime or not nature of the number...

I also know that it should be counter += 2... because only odd numbers are prime beyond the number 2.
hm... i changed the counter like i just mentioned above... and now it works fine... at least i think it does... i have to test it a little bit more...
closed account (1vRz3TCk)
Just a small note: 1 is not a prime number.
nevermind... that breaks it for other numbers...
quite right... it's not CodeMonkey...
Last edited on
closed account (1vRz3TCk)
mjmckechnie,
Had a bit of a rearrange of you code:
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
#include <iostream>
#include <cmath>

int main()
{
    unsigned int number = 0;
    
    std::cout << "Please enter a positive integer: "; 
    std::cin >> number; 
        
    bool prime = true;
    if (number < 2) 
        prime = false;
    else if (number == 2)
        prime = true;
    else if (number % 2 == 0)
        prime = false;
    else if (number > 2)
    {
        unsigned int counter = 3;
        double number_as_double = static_cast<double>(number);
        unsigned int upper_limit = static_cast<unsigned int> (sqrt( number_as_double  ) ); 

        while(counter <= upper_limit)
        {
            if (number % counter == 0)
            {
                prime = false;
                break;
            }
            counter +=2; 
        }

    }

    if(prime == true)
    {
        std::cout << "The number " << number << " is a prime number..." << std::endl << std::endl;
    }
    else
    {
        std::cout << "The number " << number << " is not a prime number..." << std::endl << std::endl;
    }

    system("PAUSE");

    return 0;
}
Last edited on
that actually looks pretty good! Sorry for late reply.... had to go to bed.... and now I have to go to work.... but thank you for your help .... I'm checking this out when I get off :-)
Here is my code check this may this help you
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
// prime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int num;
	
	
	char ch;
	do
	{
		int prime=0;
	cout<<"Enter a number ";
	cin>>num;
	for(int i=1;i<=num/2;i++)
	{
		if(num%i==0)
		{
			prime++;
		}
		else
		{
		
		}

	}
	if(prime>1)
		cout<<"Number is not prime "<<num<<endl;
	else
		cout<<"Number is prime "<<num<<endl;

	cout<<"Do you want to do it again ";
	cin>>ch;
	}
	while(ch=='y' || ch=='Y');
	return 0;
}

Topic archived. No new replies allowed.