Prime Number Generator.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • willistwigge
    New Member
    • Feb 2010
    • 1

    Prime Number Generator.

    Good Afternoon,

    I am trying to create a program that generates all the prime numbers from 2 to n, an intiger input by the user. I tried to accomplish this by using a function that i created by the name is_prime:

    Code:
    #include <stdio.h>
    #include <stdlib.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;}
    I want to include a counter in my program so that the final output looks something like this:
    1: 2
    2: 3
    3: 5
    4: 7
    5: 11
    6: 13
    7: 17
    etc.

    I would really appreciate some help with the actual function.
    Thanks in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What is the problem? The function you have posted looks like it should work.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      You are identifying the prime but you are not printing it out.
      Some where in there you need to declare and initialize (int count=0), increment it (count++) after each iteration and print it.

      Comment

      Working...