prime # query

This is a discussion on prime # query within the C Programming forums, part of the General Programming Boards category; I have a probelm. I am trying to get my program to determine if a value entered is prime or ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    Question 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;
    }

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: prime # query

    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;
    }
    hello, internet!

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    I've tried it but its still not working properly. am i doing something else wrong that i cant see?

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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
    hello, internet!

  5. #5
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    /* Every even number is not prime */
    2 is prime

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    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;
    }

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    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;
    }

  8. #8
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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
    hello, internet!

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 11:06 AM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 03:32 PM
  4. Prime Wonder
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-07-2002, 11:49 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21