Outputting numbers issue

I am having some issues finalising a program which outputs either prime numbers or perfect numbers, depending on what the user desires. The limits are between 2-1000 for the prime numbers and 1-1000 for the perfect numbers.

I have done the prime numbers part, though am having some difficulties with the perfect numbers, and I can't quite work out what is going wrong.

Any help / advice would be much appreciated.

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
79
80
81
82
83
#include <iostream>
using namespace std;


bool Prime_Tester(int n);
bool Perfect_Tester(int n);


int main()
{
	int choice;

	cout << "For Perfect Numbers between 1 and 1000, please enter 1." << endl;
	cout << "For Prime Numbers between 2 and 1000, please enter 2." << endl;
	
	cin >> choice;

	switch (choice)
	{
	case 1:
	{
		cout << "You have selected Perfect Numbers" << endl;
		for (choice = 1; choice <= 500; choice++)
		{
			if (Perfect_Tester(choice))
				cout << choice << " ";
		}
	}
		break;

		case 2:
		{
			cout << "You have selected Prime Numbers." << endl;
			for (choice = 2; choice <= 1000; choice++)
			{
				if (Prime_Tester(choice))
					cout << choice << " ";
			}
		}
	break;

		default:
		{
			cout << "Please enter a valid number." << endl;
		}
	}
	return 0;
}


bool Prime_Tester(int n)
{
	int i;
	int divisor;
	divisor = 0;
	for (i = 1; i <= n; i++)
		{
			if (n%i == 0)
				++divisor;
		}
		if (divisor == 2)
			return true;
		return false;
}


bool Perfect_Tester(int n)
{
	int sum;
	int divisor;
	sum = 0;
	divisor = 500;
		if (divisor % n == 0)
		{
			sum += n;
		}

		if (sum == n)
		{
			return true;
		}
	return false;
}
Last edited on
What is a perfect number?

Aceix.
Sorry, I should have included that.

A positive integer is perfect if it is equal to the sum of its positive factor. Eg. 6 is perfect because its factors are 1, 2 and 3 and 1+2+3 = 6.
A rough idea is to check if all its factors sum up to the number itself, by iteration.

Aceix.
Topic archived. No new replies allowed.