# problem with my prime number program

• 07-11-2002
datainjector
problem with my prime number program
well this program was made so it could tell u the if the number entered is a prime number or not...but i aint workin'

Code:

```#include <stdio.h> int prime_number ( int ); int main() {  int num1, sum;   printf ("Enter a number:");   scanf ("%d", &num1 );         sum = prime_number ( num1 );     if ( sum == 1){     printf ("%d is not a prime number", num1 );     else         if ( sum == 0 )           printf ("%d is a prime number", num1) return 0; } int prime_number ( int a) {   int num2, tot;   num2 = a/2;   tot = a/num2;       if ( tot == 2 )         return 1;     else         return 0; }```
• 07-11-2002
foniks munkee
Code:

```if ( sum == 1){ .... printf ("%d is a prime number", num1)```
Well for a start you don't close of the bracket at the end of your if statement and you don't have a semi colon to close off the statement on your printf.

If it is just not compiling this should fix it. Otherwise let us know.
• 07-11-2002
foniks munkee
Your logic isn't quite correct either as 2 and 19 are prime numbers, but the program will say they aren't.

Definition: An integer p is called a prime number if the only positive integers that divide p are 1 and p itself.

This function should do it, please note that you will have to include the math.h library for the functions floor and sqrt.
Code:

```int prime_number(int n) {   int i;   for (i = 2 ; i <= floor(sqrt(n)) ; i++) {     if (n % i == 0) {       return(0);     }   }   return(1); }```
• 07-11-2002
Prelude
>but i aint workin'
There are several syntax errors that keep it from compiling. But past that your computations are off. Consider how one would simply calculate whether a number is prime:
Code:

```/* Pseudocode: I hold no responsibility for errors syntactic, logical, or mathematical ;) */ int main ( void ) {   int val, i, flag = 1;   printf ( "Enter a number: " );   if ( scanf ( "%d", &val ) == 1 ) {     if ( val == 1 ) flag = 0;     for ( i = 2; i < val; i++ )       if ( i < val && val % i == 0 )         flag = 0;     printf ( "%d is %sprime\n", val,       flag != 1 ? "not " : "" );   }   return 0; }```
Loop through all values less than your value. If your value is divisible by any of them then it is not prime. This is a slower method, but it should work for what you need.

-Prelude
• 07-12-2002
Shiro
The algorithm in your function prime_number does not check if a number is prime, but it checks in a strange way if numbers are odd or even.

Code:

```int prime_number ( int a) {   int num2, tot;   num2 = a/2;   tot = a/num2;       if ( tot == 2 )         return 1;     else         return 0; }```
What you do is, in theory, something like this:

num2 = a / 2
tot = a / num2 = a / (a / 2) = a * (2 / a) = 2a / a = 2

In practice, there is a round-off error when dividing, so num2 is not always equal to a/2, but approximately a/2. In some cases a/num2 should give 2, but because of approximation, it will not always do.

When designing algorithms, always use pen and paper to work them out on paper before coding.