C checking for prime number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • silversnake
    New Member
    • Mar 2007
    • 11

    C checking for prime number

    I'm trying to write a program that take a input number and prints if is a prime numbers but is not working for instance, it says that 4 is prime while 5 is not.
    can anyone see what the problem is . thanks in advance


    #include <stdio.h>
    #include <math.h>

    #define TRUE 1;
    #define FALSE 0;

    void getNumber();

    int main()
    {
    int number;

    getNumber(&numb er);

    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) = 2)
    {
    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;
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by silversnake
    I'm trying to write a program that take a input number and prints if is a prime numbers but is not working for instance, it says that 4 is prime while 5 is not.
    can anyone see what the problem is . thanks in advance


    #include <stdio.h>
    #include <math.h>

    #define TRUE 1;
    #define FALSE 0;

    void getNumber();

    int main()
    {
    int number;

    getNumber(&numb er);

    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) = 2)
    {
    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;
    }
    I see a few problems:
    Code:
    if (scanf("%d", &number) = 2)
    Here you set the number to equal 2. You want '==' to make a conditional.

    Code:
       if (number % 2 == 0) return TRUE;
    You return true for an even number? On a function titled 'isPrime()'? I believe you want the '!=' operator there.

    If you're still getting odd values after that, I would recommend using parentheses around the individual statements, to show order of operations.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by sicarie
      I see a few problems:
      Code:
      if (scanf("%d", &number) = 2)
      Here you set the number to equal 2. You want '==' to make a conditional.
      I don't think this sets number, on the other hand I am surprised it compiles as I believe it is trying to set the return value of the function scanf which is not an lvalue.

      Originally posted by sicarie
      Code:
         if (number % 2 == 0) return TRUE;
      You return true for an even number? On a function titled 'isPrime()'? I believe you want the '!=' operator there.
      On the right lines but not quite right, just because it isn't divisible by 2 doesn't make it prime!


      Hi silversnake, your function is returning the logical inverse (i.e. true for false and false for true) of what it names suggests (the first clue is in the numbers you give 4 (not prime) as prime 5 (prime) as not prime). It would be more aptly named isNotPrime.

      Comment

      • emilchacko
        New Member
        • Mar 2007
        • 1

        #4
        this is not da way u do prime chk.what u have 2 do is take a no. and chk with all its preceding no.s if it is divisible or not if so in any case( break;) after setting a flag=1. And chk if flg is 1 then not prime else otr way . this is da simplest way there are other methods also.try this.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by emilchacko
          this is not da way u do prime chk.what u have 2 do is take a no. and chk with all its preceding no.s if it is divisible or not if so in any case( break;) after setting a flag=1. And chk if flg is 1 then not prime else otr way . this is da simplest way there are other methods also.try this.
          Actually once the mistakes are removed (I just spotted an erroneous ; following a for statement) the algorithm the OP has tried to implement works fine with the 1 exception of accidentally identifying the number 2 as not prime.

          Comment

          • silversnake
            New Member
            • Mar 2007
            • 11

            #6
            thanks for the help , it works now

            Comment

            Working...