Perfect numbers!!!

I need to make a program wich finds all prefect numbers in between two numbers given by the user: 1-51 = 6 and 28 are prefect,but I dont know how, I made a program wich only test one number,given by the user! Can i get help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>
using namespace std;
int main()
{
	int number, i, sum;
	cout<<"Input a number to see is he a prefect number!\n";
	cin>> number;
	sum=1;
	for(i=2; i<=(number/2);i++)
	{
		if(number%i==0)
		sum+=i;
		
	}
	if(number==sum)
	
	cout<<"Number "<<number<<" is perfect!";
	else cout<<"Number "<<number<<" is not prefect";
	return 0;
		
	
	
}
Have you learned functions yet?
See http://www.cplusplus.com/doc/tutorial/functions/

You do know how to test one number. Encapsulate that procedure in a function. The use of functions keeps the main() simpler.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void testperfect( int value )
{
  // what to do here in order to get the
  // same result as your program does?
}

int main()
{
  int number;
  cout<<"Input a number to see is he a prefect number!\n";
  cin>> number;

  testperfect( number );

  return 0;
}



Now we can focus on the main(). You should get not one, but to numbers, which define a range. You must test each value in the range. Call the testperfect() with each value in the range. Repeat. Iterate. Loop.
closed account (48T7M4Gy)
@OP I don't want to discourage you but there aren't all that many and to find them beyond the first few you will need to use more than an int:

https://en.wikipedia.org/wiki/List_of_perfect_numbers
Topic archived. No new replies allowed.