Thread: Can someone help with this prime code?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    40

    Can someone help with this prime code?

    I have an error with this code and i'm not sure whats wrong with it here's the errors:

    Code:
    primes.c: In function `main':
    primes.c:19: parse error before `;'
    primes.c: At top level:
    primes.c:26: parse error before `}'
    here's the actual code:

    Code:
    /*  Ryan Perrott
        CMSC 104 0201
        Lab 7
        11/21/02
        Due: 11/24/02
        Description: This program uses a function to determine if the number is
                     prime and then prints out all the prime numbers between 1-10000.
    */
    
    #include <stdio.h>
    #include <math.h>
    
    int prime( int ); /* function prototype */
    
    int main()
    {
        int x;
    
        for(x=1; x<=10000; x++;)
          {
              if( prime( x ) == 1;
              {
                  printf( "%d\n", x );
              }
          }
    }
    
    int prime( int n )
    {
        int a_prime;
        int i;
        float high;
    
        a_prime = 1;
        if ((n%2)==0)
          {
             a_prime = 0;
          }
        high = sqrt((float) n);
        for (i=3; i<=high; i=i+2)
          {
            if ((n%i)==0)
              {
                  a_prime = 0;
              }
          }
        return a_prime;
    
    }
    any help would be appreciated thanks!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Can someone help with this prime code?

    Maybe it's my eyes, but something seems to be missing...
    Originally posted by blindleaf
    Code:
    int main()
    {
        int x;
    
        for(x=1; x<=10000; x++;) <--
          {
              if( prime( x ) == 1; <--
              {
                  printf( "%d\n", x );
              }
          }
    }
    Last edited by Magos; 11-21-2002 at 03:09 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    ok i got rid of the ";" after the "x++" and i've added another ")" to finish off the if statement, but now i'm getting this error:

    Code:
    /tmp/ccoKCW7q.o: In function `prime':
    /tmp/ccoKCW7q.o(.text+0x81): undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    whats wrong with it now?

    here's the code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int prime( int ); /* function prototype */
    
    int main()
    {
        int x;
    
        for(x=1; x<=10000; x++)
          {
              if( prime( x ) == 1);
              {
                  printf( "%d\n", x );
              }
          }
    }
    
    int prime( int n )
    {
        int a_prime;
        int i;
        float high;
    
        a_prime = 1;
        if ((n%2)==0)
          {
             a_prime = 0;
          }
        high = sqrt((float) n);
        for (i=3; i<=high; i=i+2)
          {
            if ((n%i)==0)
              {
                  a_prime = 0;
              }
          }
        return a_prime;
    
    }

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    ok it compiled now but can someone help me with the logic? The ouput is every number 2-10000 i only want the primes i thought thats what i wrote the program to do, but its not doing that. Do i have some sort of syntax errror? please someone help me.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Code:
    if( prime( x ) == 1); <---

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    thank you, i'm an idiot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime number program code
    By jd7joe in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2005, 10:14 AM
  2. Replies: 18
    Last Post: 11-04-2005, 02:41 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM