# prime # query

• 09-03-2002
alokin
prime # query
I have a probelm. I am trying to get my program to determine if a value entered is prime or not. My problem is that whichever number is entered it always says that the number is prime. Can someone take a look at my code and explain what i am doing wrong.

Code:

```#include <stdio.h> #include <math.h> #define TRUE 1; #define FALSE 0; void getNumber(int *number); int isPrime(int number); int main() {   int number;   getNumber(&number);   if (isPrime(number))       printf("\n%d is a prime number\n", number);   else       printf("\n%d is not a prime number\n", number);         return 0; } void getNumber(int *number) {   printf("Please enter a positive number ");   if (scanf("%d", number) != 1)   {       printf("Invalid number entered\n");       exit(1);   } } int isPrime(int number) {   int count, s;   /* Every even number is not prime */   if (number % 2 == 0) return TRUE;   /* check every odd number up to the square root of the number */   s = sqrt(number);   for (count=3; count<=s; count+=2);   {       if (number % count == 0) return TRUE;   }   return FALSE; }```
• 09-03-2002
moi
Re: prime # query
Quote:

Originally posted by alokin
I have a probelm. I am trying to get my program to determine if a value entered is prime or not. My problem is that whichever number is entered it always says that the number is prime. Can someone take a look at my code and explain what i am doing wrong.

Code:

```#include <stdio.h> #include <math.h> #define TRUE 1; #define FALSE 0; void getNumber(int *number); int isPrime(int number); int main() {   int number;   getNumber(&number);   if (isPrime(number))       printf("\n%d is a prime number\n", number);   else       printf("\n%d is not a prime number\n", number);         return 0; } void getNumber(int *number) {   printf("Please enter a positive number ");   if (scanf("%d", number) != 1)   {       printf("Invalid number entered\n");       exit(1);   } } int isPrime(int number) {   int count, s;   /* Every even number is not prime */   if (number % 2 == 0) return TRUE;   /* check every odd number up to the square root of the number */   s = sqrt(number);   for (count=3; count<=s; count+=2);   {       if (number % count == 0) return TRUE;   }   return FALSE; }```

• 09-03-2002
alokin
I've tried it but its still not working properly. am i doing something else wrong that i cant see?
• 09-03-2002
moi
Quote:

Originally posted by alokin
I've tried it but its still not working properly. am i doing something else wrong that i cant see?
yeah you mixed up your return TRUE and return FALSE lines
• 09-03-2002
Driveway
Quote:

/* Every even number is not prime */
2 is prime
• 09-03-2002
alokin
It still isnt working properly, i have made changes. Any other ideas where my flaw is?

Code:

```#include <stdio.h> #include <math.h> #define TRUE 1; #define FALSE 0; void getNumber(int *number); int isPrime(int number); int main() {   int number;   getNumber(&number);   if (isPrime(number))       printf("\n%d is a prime number\n", number);   else       printf("\n%d is not a prime number\n", number);         return 0; } void getNumber(int *number) {   printf("Please enter a positive number ");   if (scanf("%d", number) != 1)   {       printf("Invalid number entered\n");       exit(1);   } } int isPrime(int number) {   int count, s;   /* Every even number is not prime except for '2' */   if (number % 2 == 0 && number != 2) return FALSE;   /* check every odd number up to the square root of the number */   s = sqrt(number);   for (count=3; count<=s; count+=2);   {       if (number % count == 0) return TRUE;   }   return TRUE; }```
• 09-03-2002
alokin
I have tried to change things but i am still getting that all even numbers exept 2 are not prime, but every odd number is also prime. Is there something wrong with my loop? please help

Code:

```#include <stdio.h> #include <math.h> #define TRUE 1; #define FALSE 0; void getNumber(int *number); int isPrime(int number); int main() {   int number;   getNumber(&number);   if (isPrime(number))       printf("\n%d is a prime number\n", number);   else       printf("\n%d is not a prime number\n", number);                 scanf("%d", number);   return 0; } void getNumber(int *number) {   printf("Please enter a positive number ");   if (scanf("%d", number) != 1)   {       printf("Invalid number entered\n");       exit(1);   } } int isPrime(int number) {   int count, s;   /* Every even number is not prime except for '2' */   if (number % 2 == 0 && number != 2) return FALSE;   /* check every odd number up to the square root of the number */   s = sqrt(number);   for (count=2; count<=s ; count++);   {       if (number % count == 0) return TRUE;   }   return TRUE; }```
• 09-03-2002
moi
think about it. if the (number % 2) == 0 then it is not prime and you should return FALSE (unless it is 2 itself).

if the (number % anyinterger other than 1 and itself) == 0 then it is not prime and you should also return FALSE.

otherwise return TRUE.

and you didnt remove the semicolon in the for statement that i already pointed out
• 09-04-2002
Shiro
Take a closer look at this part of the code:

Code:

```  /* check every odd number up to the square root of the number */   s = sqrt(number);   for (count=2; count<=s ; count++);   {       if (number % count == 0) return TRUE;   }   return TRUE;```

....

(If number is divisible by count, then you return TRUE)