Finding Prime Numbers...What's wrong with my program?

I'm kind of stumped on this one - I'm not sure what I'm doing wrong. Everything in my program is working fine, except for determining prime numbers. For the prime number section, if it's not prime I have an "else-if," but I had an "else" there fore a while, so I'm not sure that either of those make a difference.

Thanks for looking.

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
#include <iostream>

using namespace std;

int main()
{

    int userNum = 0, sum = 0;

    cout << "\nThis program will take numbers entered by a user, and will determine if a number is a prime number or a perfect number. Enter -99 when you'd like to exit. " << endl;

    do {

	int userNum = 0, sum = 0;


	cout << "\nPlease enter a number between 1 and 1000: \n";
	cin >> userNum;

	while (userNum < 1 || userNum > 1000) {

	    cout << "\nPlease enter a number between 1 and 1000: \n";
	    cin >> userNum;

	}
	
    // Output divisors whether number is prime or not.

	cout << "\nDivisors: \n";

	for (int i = 1; i <= userNum; i++) {
	    if (userNum % i == 0) {
		cout << i << endl;
	    }
	}

	for (int i = 1; i <= userNum; i++) {
	    if (userNum % i == 0) {
		cout << "\nThis number is not a prime number. \n";
		break;
	    } else if ((userNum / 1 == userNum) && (userNum / userNum == 1)) {
		cout << "\nThis number is a prime number. \n";
	    }
	}

	// Output divisors whether number is perfect or not.

	cout << "\nDivisors: \n";

	for (int i = 1; i <= userNum / 2; i++) {
	    if (userNum % i == 0) {
		sum += i;
		cout << i << endl;
	    }
	}
	// Check for a perfect number - The number must be equal to the sum of it's divisors. If 'k' is a divisor, it will be added up to sum.

	if (sum == userNum)
	    cout << "\nThe number " << userNum << " is a perfect number. \n";
	else
	    cout << "\nThe number " << userNum << " is not a perfect number. \n";
    }
    while (userNum != -99);
    return 0;
}
Last edited on
closed account (o3hC5Di1)
Hi there,

Let's look at your code for prime numbers:

1
2
3
4
5
6
7
8
	    for (int i = 1; i <= userNum; i++) {
	    if (userNum % i == 0) {
		cout << "\nThis number is not a prime number. \n";
		break;
	    } else if ((userNum / 1 == userNum) && (userNum / userNum == 1)) {
		cout << "\nThis number is a prime number. \n";
	    }
	}


Now, the definition for a prime number is that it is only divisible by 1 and itself.
Every number, however, can be divided by 1 and itself, so checking for that in your loop doesn't make much sense. So really, all you need to do is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
bool prime = true;
for (int i = 1; i <= userNum; i++) { //edited as per Chervil's comment
for (int i = 2; i < userNum; i++) {
	    if (userNum % i == 0) {
		prime=false;
		break;
	    }
	}

if (prime)
    cout << "\nThis number is a prime number. \n";
else
    cout << "\nThis number is not a prime number. \n";



Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Last edited on
> Every number, however, can be divided by 1 and itself, so checking for that in your loop doesn't make much sense.
1
2
3
4
5
6
7
8
9
10
    bool prime = (userNum>1);
    
    for (int i = 2; i < userNum; i++) 
    {
        if (userNum % i == 0) 
        {
            prime=false;
            break;
    	}
    }
closed account (o3hC5Di1)
Ah yes, thanks very much for your correction Chervil - I forgot to adapt the code I copied from the OP.

All the best,
NwN
Hello, sorry for the delay - Was busy with other classes...Still not getting it. Maybe I missed something in my code. I always get that it's not a prime number, no matter what number I put.

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
#include <iostream>

using namespace std;

int main()
{

  	int userNum = 0, sum = 0;

    cout << "\nThis program will take numbers entered by a user, and will determine if a number is a prime number or a perfect number. Enter -99 when you'd like to exit. " << endl;

    do {

	int userNum = 0, sum = 0;

	cout << "\nPlease enter a number between 1 and 1000: \n";
	cin >> userNum;

	while (userNum < 1 || userNum > 1000) {

	    cout << "\nPlease enter a number between 1 and 1000: \n";
	    cin >> userNum;

	}

	cout << "\nDivisors: \n";

	for (int i = 1; i <= userNum; i++) {
	    if (userNum % i == 0) {
		cout << i << endl;
	    }
	}


	bool Prime = true;

	for (int i = 1; i <= userNum; i++) {

	    if (userNum % i == 0) {
		Prime = false;
		break;
	    }
	}

	if (Prime)

	    cout << "\nThis number is a prime number. \n";

	else

	    cout << "\nThis number is not a prime number. \n";

	// Output divisors whether number is perfect or not.

	cout << "\nDivisors: \n";

	for (int i = 1; i <= userNum / 2; i++) {
	    if (userNum % i == 0) {
		sum += i;
		cout << i << endl;
	    }
	}

	// Check for a perfect number - The number must be equal to the sum of it's divisors. If 'k' is a divisor, it will be added up to sum.

	if (sum == userNum)
	    cout << "\nThe number " << userNum << " is a perfect number. \n";
	else
	    cout << "\nThe number " << userNum << " is not a perfect number. \n";
    }

    while (userNum != -99);
    return 0;
}
closed account (o3hC5Di1)
Hi there,

I suggest you read the posts above again - your for loop still is not adapted accordingly.

All the best,
NwN
Topic archived. No new replies allowed.