Finding Perfect Numbers and outputting their divisors

So, I have a project for a class, where I have to take the input of several numbers and check if they are prime, or perfect. I've been able to get the program to check Prime numbers accurately, but have been lurking and unable to figure out a way to calculate a perfect number. We're also supposed to have error checking aswell to make sure inputted numbers are valid, and the current one I have in place works, but only if I limit the input to a single number

We're supposed to also allow a series of numbers to be inputed, and have each number checked, but our Professor hasn't gone over exactly how to do that in any depth, and I'm a bit lost at the moment. I've added what I have so far in the bottom.
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
84
85
86
87
88
89
90

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	int num1, numcheck = 0, primecheck = 0, perfcheck = 0, divider, repeat = 1;
	int primcounter = 0, perfcounter = 0; //primcounter for prime cycles. perfcounter for perfect cycles
	//good god this is going to hurt

	cout << "Enter a number into this program and it will calculate whether it is a prime " << endl;
	cout << "number or perfect number." << endl;
	cout << "Please use only positive numbers, as it is impossible to have a negative prime" << endl;
	cout << "or positive number" << endl;
	cout << "If you try to enter a negative number, you will be prompted to try again" << endl;
	cout << endl;
	while (repeat == 1)
	{
		repeat = 0;

		while (numcheck == 0)
		{
		cout << "Please enter an integer between 1 - 1000, that you wish to be processed." << endl;
		cin >> num1;
		cout << endl;
		if (num1 <= 0)
		{
			if (num1 == 0)
			{
				cout << "You have entered the number zero. This is an invalid entry. Please try again" << endl;
			}
			else
			{
				cout << "You have entered a negative number. Please try again" << endl;
				cout << "Please remember, prime and perfect numbers can ONLY be positive" << endl;
			}
		}
		else
		{
			if (num1 > 1000)
			{
				cout << "You have entered an integer above the stated threshold. Your inputed interger was " << num1 << endl;
				cout << "Please re-enter your selection again, choosing a number between 1 - 1000" << end;
			}
			else
			{
				cout << "Thank you!" << endl;
				numcheck = 1;
			}
			
		}

		}//this is the end of the NumberCheck to ensure the user has inputted a positive number

	for (int i = 2; i < num1; i++)
	{
		if (num1%i == 0)
		{
			primcounter++;
		}

	}
	if (primcounter == 0)
	{
		cout << "The number " << num1 <<" is a prime number!" << endl;
		primecheck = 1;
		cout << "Would you like to check another number? Enter 1 to repeat, 0 to exit" << endl;
		cin >> repeat;
	}
	else
	{
		cout << "The number " << num1 << " is a not prime number!" << endl;
		primecheck = 0;
		cout << "Would you like to check another number? Enter 1 to repeat, 0 to exit" << endl;
		cin >> repeat;
	}
	primecheck = 0;
	primcounter = 0;
	numcheck = 0;
	perfcheck = 0;
}




	system("pause");
	}
Include this in your code

1
2
3
4
5
6
7
8
9
10
11

       while(i<num){
       if(num%i==0)
       sum1=sum1+i1;
       i1++; 
}
 
if(sum1==num)
    cout << i1 << " is a perfect number\n"; 
else
    cout << i1 << " is not a perfect number\n";
Thanks Programmer. I'm trying to go through the code you gave me to understand how it works. What is i1 assigned to, as I'm not using any assignment for that variable?
oh sorry!

i and i1 are same...... I game the different names by mistake
No worries. Still trying to figure out how to implement it with the current variables I have. Beginner for C++ for my degree, and I'm absolutely lost on how to implement it.
I'm having a bit of trouble tracing the logic of the calculation
beauty of this language is that its similar (sometimes better) to maths..... just understand the logic with a pen and paper an then you can make out the logic of calculation.....
Actually finally figured out the logic behind it.

if num1 divided by i has a remainder of 0, i++, and add sum1+i to sum1. Repeat

If sum1 is equal to num1, the number is perfect.

However, when I implement it into my code, every number is perfect that is not a prime number.

I know 8 is not a perfect number, but my code is telling me that it is.

I chose to continue the logic flow to if the number failed to be prime, it would check if it is a perfect number. And after that, I'd be adding in a function to output its divisors. But I'm having the problem of it telling me all numbers are perfect.........
Updated code is here

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	int num1, sum1, numcheck = 0, primecheck = 0, perfcheck = 0, divider, repeat = 1, i ;
	int primcounter = 0, perfcounter = 0; //primcounter for prime cycles. perfcounter for perfect cycles
	//good god this is going to hurt

	cout << "Enter a number into this program and it will calculate whether it is a prime " << endl;
	cout << "number or perfect number." << endl;
	cout << "Please use only positive numbers, as it is impossible to have a negative prime" << endl;
	cout << "or positive number" << endl;
	cout << "If you try to enter a negative number, you will be prompted to try again" << endl;
	cout << endl;
	while (repeat == 1)
	{
		repeat = 0;

		while (numcheck == 0)
		{
		cout << "Please enter an integer between 1 - 1000, that you wish to be processed." << endl;
		cin >> num1;
		cout << endl;
		if (num1 <= 0)
		{
			if (num1 == 0)
			{
				cout << "You have entered the number zero. This is an invalid entry. Please try again" << endl;
			}
			else
			{
				cout << "You have entered a negative number. Please try again" << endl;
				cout << "Please remember, prime and perfect numbers can ONLY be positive" << endl;
			}
		}
		else
		{
			if (num1 > 1000)
			{
				cout << "You have entered an integer above the stated threshold. Your inputed interger was " << num1 << endl;
				cout << "Please re-enter your selection again, choosing a number between 1 - 1000" << endl;
			}
			else
			{
				cout << "Thank you!" << endl;
				numcheck = 1;
			}
			
		}

		}//this is the end of the NumberCheck to ensure the user has inputted a positive number

		//check if a number is prime, by seeing total remainders when divided between 1 and the inputed number. If total == 2, then the number is prime. If more, it is not prime
	for (int i = 2; i < num1; i++)
	{
		if (num1%i == 0)
		{
			primcounter++;
		}

	}
	if (primcounter == 0)
	{
		cout << "The number " << num1 <<" is a prime number!" << endl;
		primecheck = 1;
		cout << "Would you like to check another number? Enter 1 to repeat, 0 to exit" << endl;
		cin >> repeat;
	}
	else
	{
		cout << "The number " << num1 << " is a not prime number!" << endl;
		sum1 = 0;
		for (i = 1; i < num1; i++)
		{
			if (num1%i == 0)
			{
				sum1 = sum1 + i;
			}
		}
		if (sum1 == num1)
		{
			cout << "The number " << num1 << " is a perfect number" << endl;
			cout << "Would you like to check another number? Enter 1 to repeat, 0 to exit" << endl;
			cin >> repeat;
		}
		else
		{
			cout << "The number " << num1 << " is a perfect number" << endl;
			cout << "Would you like to check another number? Enter 1 to repeat, 0 to exit" << endl;
			cin >> repeat;
		}

	}
	primecheck = 0;
	primcounter = 0;
	numcheck = 0;
	perfcheck = 0;
	i = 0;
}




	system("pause");
	}
Last edited on
Topic archived. No new replies allowed.