## koman 36

``````#include <iostream>
using namespace std;

int main ()

{

int j;
int t;
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 = 2; j > p; j++ )

if ( p % j != 0 )
t = p;

cout << t << endl;

}

return 0;

}``````

I'm using this program to give me the prime numbers that are less than the number entered, but it's not working?

## VernonDozier 2,218

``````#include <iostream>
using namespace std;

int main ()

{
int j;
int t;
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 = 2; j > p; j++ )
if ( p % j != 0 )
t = p;

cout << t << endl;
}

return 0;
}``````

You do realize that the top for-loop starts on line 14 and goes to line 18? Lines 20 - 26 are not part of the for-loop that starts on line 14.

## koman 36

``````#include <iostream>
using namespace std;

int main ()

{
int j;
int t;
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 = 2; j > p; j++ )
if ( p % j != 0 )
t = p;

cout << t << endl;
}

return 0;
}``````

You do realize that the top for-loop starts on line 14 and goes to line 18? Lines 20 - 26 are not part of the for-loop that starts on line 14.

ok how do I fix that?

## chococrack 74

wrap it in braces
{

blah

}

Also, in your if statement, I think you're missing an else?

``````if   ( ( i == 2 ) or  ( i % 2  != 0  ) )
{
p = i;
}
else  // ????
{
for ( int j = 2; j > p; j++ )
{
if ( p % j != 0 )
t = p;
}

cout << t << endl;
}``````

## VernonDozier 2,218

ok how do I fix that?

You need brackets around what you want in the for-loop. No brackets means that only the next instruction is part of the loop. Put brackets around what needs to be repeated:

``````for (/* for-loop code */)
{
// code to be repeated.  Inside brackets.
}
// code that is not repeated.  Outside the brackets.``````

## koman 36

You need brackets around what you want in the for-loop. No brackets means that only the next instruction is part of the loop. Put brackets around what needs to be repeated:

``````for (/* for-loop code */)
{
// code to be repeated.  Inside brackets.
}
// code that is not repeated.  Outside the brackets.``````

I've tried chaning the brackets and it still doesn't work.

## VernonDozier 2,218

I've tried chaning the brackets and it still doesn't work.

Post what you've tried.

## stilllearning 148

I am curious ..

how did you get this to compile ? `if ( ( i == 2 ) or ( i % 2 != 0 ) )` There is no keyword called "or" in C++ its denoted by "||"

## stilllearning 148

``````for ( int i = 2 ; i < num; i++ )
/* you can add a special case for 2, you only need to check for the factors of
odd numbers which are greater than 2 */
if   ( ( i == 2 ) or  ( i % 2  != 0  ) )
{
/* you don't really need this placeholder. you can just use "i" */
p = i;
}

{
/* this doesn't make sense... you should be checking from 2 upto the odd number.
j will always be less than p */
for ( int j = 2; j > p; j++ )
/* this doesn't help either, if your odd number is not divisible by the last
number then it will assume it to be prime ? */
if ( p % j != 0 )
t = p;

cout << t << endl;
}``````

Here is some pseudocode to help

``````if number is odd
for i = 2 to number
if number divisible by i
haveFactors = true;
break;
end if
end for
if haveFactors is false, we have a prime number.
end if``````

## koman 36

``````#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;
}``````

i'm trying to get the ODD numbers but its gives even numbers

## VernonDozier 2,218

``````#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?

## Salem 5,138

> I was surprised, but this code compiled on Dev C++. I figured "or" would be an error
http://david.tribble.com/text/cdiffs.htm#C99-alt-tok

It's one of the less travelled paths through the C++ standard :)

commented: Interesting. Good find. +8