primes program need some help

This is a discussion on primes program need some help within the C Programming forums, part of the General Programming Boards category; Okay i got this primes program working. I have the three files working together to give me a program that ...

  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,682
    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, 09: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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21