#include <iostream>
using namespace std;
int main ()
{
int q;
int j;
int sum = 0;
int p;
int num;
cout << " Please enter an even integer greater than 2: ";
cin >> num;
for ( int i = 2; i < num; i++ )
{
if ( ( i == 2 ) or ( i % 2 != 0 ) )
p = i;
for ( int j = 1; j <= p; j++ )
{
if ( p % j == 0 )
{
sum += j;
}
}
{
if ( sum == p + 1 )
q = p;
}
cout << q << endl;
}
return 0;
}
The brackets on lines 34 and 39 have no effect. The spacing makes it very hard to read. Indent consistently, get rid of the meaningless brackets and the blank lines. Here's your code with better spacing:
#include <iostream>
using namespace std;
int main ()
{
int q;
int j;
int sum = 0;
int p;
int num;
cout << " Please enter an even integer greater than 2: ";
cin >> num;
for ( int i = 2; i < num; i++)
{
if ( ( i == 2 ) or ( i % 2 != 0 ) )
p = i;
for ( int j = 1; j <= p; j++ )
{
if ( p % j == 0 )
sum += j;
if ( sum == p + 1 )
q = p;
}
cout << q << endl;
}
return 0;
}
Reread stilllearning's earlier post about line 17. I was surprised, but this code compiled on Dev C++. I figured "or" would be an error, but apparently Dev C++ didn't mind. Still, best to use || instead of "or".
I don't understand what this alrgorithm has to do with prime numbers. For example, what does sum represent?
Reputation Points: 2614
Solved Threads: 687
Posting Expert
Offline 5,374 posts
since Jan 2008