Code:
#include <iostream.h>
Should use this one if possible:
Code:
#include <iostream>
using namespace std;
Code:
void main()
{
...
}
The main function should always return an int:
Code:
int main()
{
...
return 0;
}
Code:
int x;
...
cin>>x; //so that the program doesn't close
Usually you would just do this:
Code:
cin.get(); // So that program does not close
That way you don't need an extra junk/garbage variable.
You should make a function out of your code that determines if a number is perfect or not. Have it return a bool (true/false) value. Also, your code does not look correct based on your description of what a perfect number is, here is some revised code:
Code:
bool IsPerfectNumber(int value)
{
int sum = 0;
for( int i = 1; i <= value/2; i++ )
{
if( value % i == 0 )
sum += i;
}
return sum == value;
}
Then you can call that function repeatedly inside of a loop check the numbers from 2 to m.
Code:
int m;
cout<<"Please enter number m:\n";
cin>>m;
for(int i=2;i<=m;i++)
{
if( IsPerfectNumber(i) )
cout << i << endl;
}