1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

prime number program in C

Discussion in 'C' started by matthughesfan1980, May 8, 2007.

  1. matthughesfan1980

    matthughesfan1980 New Member

    Joined:
    May 8, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello all i am so new and trying to learn while i serve.
    I am supposed to write a program that prints out the first n primes, where n is input by the user. I am supposed to write my program in its own directory in three files: primes.h, main.c, and is_prime.c
    This is what the output is supposed to look like:

    PRIMES WILL BE PRINTED.

    How many do you want to see? 3000

    Code:
        1:      2
        2:      3
        3:      5
        4:      7
        5:     11
       .....
       25:     97
       26:    101
     .......
     2998:  27431
     2999:  27437
     3000:  27449
     ........
    10000:  
    This is the files that I have so far:
    is_primes.h:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int   is_prime(int n);
    
    is_prime.c
    Code:
    #include "primes.h"
    
    int is_prime(int n)
    {
       int   k, limit;
    
       if (n == 2)
          return 1;
       if (n % 2 == 0)
          return 0;
       limit = n / 2;
       for (k = 3; k <= limit; k += 2)
          if (n % k == 0)
             return 0;
       return 1;
    }
    
    main.c
    Code:
    #include "primes.h"
    
    int main(void)
    {
       printf("PRIMES WILL BE PRINTED\n\n");
       printf("How many do you want to see?     \n");
       scanf("%5d", 
       return 0;
    }
    I dont know how to link it to the files and what to fill in the main.c file to make it work. Some guidance would be great
     
    Last edited by a moderator: May 8, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Please use the Code Block when you have code in the posts
    Give an appropriate Title. Your title said Visual Basic and you gave the code of C/C++. I edited it for you.
    You just happen to post in Introduce yourself and I moved to C-C++ for better response.
    Also you had the same post as an article of C-C++
     
    Last edited: May 8, 2007
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    It depends on your compiler. Read your compiler instructions, then if you don't understand them, post back, naming your system and compiler.
     
  5. aVague

    aVague New Member

    Joined:
    May 2, 2007
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    forex
    You need to compile them together or what?

    if yes, one way is to copy "is_primes.h" and "is_prime.c" in libraly of Builder or visual studio(if you have)
    to "include" - you ll find it , where your program(builder/studio) is installed. if you cant (so you have linux and "vim"), then you collect ALL this programs in compiling directory and write, as usual , "gcc..."
     

Share This Page