Thread: Hi, Can anyone help me? print prime numbers.

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Hi, Can anyone help me? print prime numbers.

    The programme should ask user how many primes does he wants and print out the counts and primes.

    My code is:

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    int  is_prime(int n);
    ************************************************** ******************************


    Code:
    #include "primes.h"
    
    int   main(void)
    {
       int  n, r, i = 1, primes, cnt = 0, how_many;
    
       printf("PRIMES WILL BE PRINTED. \n\n");
       printf("How many do you want to see? ");
       scanf("%d", &how_many);
       for (n = how_many; n > 0; --n){
            i++;
            r = is_prime(i);
          if(n > 0)
           if(r == 1){
           primes = i;
           ++cnt;
           printf("%8d%10d\n",cnt, primes);
         }
          
       }
       return 0;
    }
    ************************************************** ******************************

    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;
    }
    ************************************************** ***************
    for how_many = 9, I get:
    1 2
    2 3
    3 5
    4 7
    ************************************************** *******************

    Can any one help me correct this code?
    Many thanks.
    Last edited by kelai; 09-29-2010 at 12:19 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Kelai.

    Sorry to say, not many will want to study code that is not surrounded by code tags so it stays looking like code, instead of html text and all squished to the left margin.

    Change it by highlighting your code, and clicking on the # icon. You have to be in the advanced reply/edit window to see the # icon.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Looks like you're counting down no matter if it found a prime or not.
    You want to decrement n only during a prime.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Smile

    Quote Originally Posted by nonoob View Post
    Looks like you're counting down no matter if it found a prime or not.
    You want to decrement n only during a prime.
    It's working fine now.
    Many many many thanks.
    Last edited by kelai; 09-29-2010 at 01:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Numbers
    By SilentPirate007 in forum C Programming
    Replies: 15
    Last Post: 03-17-2010, 02:48 PM
  2. Replies: 2
    Last Post: 12-24-2009, 02:41 AM
  3. why the prime numbers cant be displayed?
    By mrdinggind in forum C Programming
    Replies: 4
    Last Post: 12-09-2009, 09:10 PM
  4. Perfect and Prime numbers
    By taggrath in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2009, 02:13 AM
  5. Help with program code re prime numbers
    By Anna Lane in forum C Programming
    Replies: 3
    Last Post: 11-16-2002, 10:48 AM