Thread: primes program need some help

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    51

    primes program need some help

    Okay i got this primes program working. I have the three files working together to give me a program that is suppose to print how many primes i ask for. THe problem i am having it never stops printing to the screen. If i say 2 to 3000.... it doesnt matter program just prints and prints to screen. I know the problem is with my counter, but where did i go wrong.


    Code:
    Primes.h file
    
     
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
     
    
    int is_prime( int n );
    
     
    
     
    
    Is_prime.c file
    
     
    
    #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;
    
    }
    
     
    
    Prime.c file
    
     
    
    #include "primes.h"
    
    int main(void)
    
    {
    
          int i = 2, n, count = 0;
    
     
    
          printf("How many prime number do you want to see? ");
    
          scanf("d", &n );
    
        while (1) {
    
                      if(is_prime( i ) ){
    
                            count++;
    
                            printf("%3d: %d \n", count, i);
    
                      }
    
                      i++;
    
                      if( count == n ) break;
    
                }
    
    return 0;
    
    }







    thanks in advance for your help

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    In fucntion main, the line :
    Code:
          scanf("d", &n );
    is missing %. should be like that :
    Code:
          scanf("%d", &n );

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    avoid while(1) instead
    Code:
    while (count < n) {
    
                      if(is_prime( i ) ){
    
                            count++;
    
                            printf("%3d: %d \n", count, i);
    
                      }
                      i++;
    }
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM