# Thread: Need help with prime number C program.

1. ## Need help with prime number C program.

I'm working on a program for a class:

Write a program that asks for an integer from the user and determines whether or not the integer is odd or even, then determines if that number is a prime number.

I have it working almost perfectly, but the problem I'm finding is that it says that 1 and 4 are prime numbers. All the other numbers i check are correct except 1 and 4. Does anyone know why this is?

Here is the code I have so far:

Code:
```#include <stdio.h>
int main ( void )
{
int integer;
int x;

printf( "Enter an integer (-1 to end): " );
scanf( "%d", &integer );

while( integer != -1 )
{

if( integer % 2 == 0 )
{
printf( "The number you have entered is an even number.\n" );
}
else
{
printf( "The number you have entered is an odd number.\n" );
}

for( x = 2; x < integer / 2; x++ )
{
if( integer % x == 0 )
{
printf( "The number you have entered is not a prime number.\n" );
break;
}
}

if( !( x < integer / 2 ))
{
printf( "The number you have entered is a prime number.\n" );
}

printf( "Enter an integer (-1 to end): " );
scanf( "%d", &integer );

}

return 0;
}```

2. I would try instead of "integer / 2" to use "((integer / 2) + 1)"

Should fix the four issue; not sure about 1.

Edit1: Or you might try "<=" instead "<"; I prefer not to use "<=" except when that is more correct.
In this case, I am not sure of the more correct choice. I might use <= or the plus 1 solution; both seems valid.

Edit2: For 1 I would just say anything smaller than 2 is not prime; so, I suggest adding an if statement doing that.

Tim S.

3. Originally Posted by stahta01

Or you might try <= instead <.

Tim S.
If you let x = integer at any time in the for loop, (x % integer) will always output zero. So the program would output that no number is prime. The < is fine.

With input 1, you would not have gone through the for loop and would therefore have
Code:
`if (!(2 < (1/2))`
The requirement for if is satisfied, it will output that it is prime.
Code:
`if (!(x < integer/2) && (integer != 1))`
^this would fix it from outputting that it is prime.

Adding 1 to (integer / 2) doesn't fix four, fyi. I will work on that later when I have time.

4. Brian29, have you learnt how to write your own functions? It would be simpler to write a function, let's call it is_prime, that returns 1 if the argument is prime, or 0 otherwise. This way, you don't need that extra check: you can just return 0 once a factor is found, and at the end of the function return 1 (though you may need an extra check for integers less than 2).

Originally Posted by Warofwest
If you let x = integer at any time in the for loop, (x % integer) will always output zero. So the program would output that no number is prime. The < is fine.
You misread stahta01's suggestion: we're talking about x <= integer / 2, not x <= integer.

5. Originally Posted by stahta01
I would try instead of "integer / 2" to use "((integer / 2) + 1)"
This actually worked to fix the 4 problem, thank you.

We have learned to write functions but I'm not very clear on how do it. I'm taking this class online and have had to learn most on my own.