Thread: prime program

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Question prime program

    I am working on quite a few exersizes in a book for a class I am taking and unfortunately, I don't think the book is any good. Every example they give has errors that you have to figure out and it has made learning this diffucult.

    I am currently working a few programs, one of them dealing with primes.

    its purpose is to write a program that prints out the first n primes, were n is inputed by the user. I also have to split it up into three folders, one being prime.h, is_prime.c, and main.c

    here is what I have so far. the problem I am having is it won't prompt the user for input and just has an error out. As far as the format of the program. It builds fine with no errors using microsoft visual.

    Code:
    prime.h
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int is_prime( int n );
    
    is_prime.c
    
    
    Code:
    #include "prime.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;
    }
    
    and finally prime.c (the main program)
    
    Code:
    #include "prime.h"
    
    
    int main(void){
    	int i=2, n, count=0;
    	
    	printf("Howw many prime number do you want to see? ");
    	scanf("%d", &n );
    	
    	printf("Primes\n------\n");
    
    	while(1){	
    		if( is_prime( i ) ){
    			count++;
    			printf("%3d: %d \n", count, i);
    		}
    		i++;
    		if( count == n ) break;	
    	}
    	return 0;
    }
    Any help anyone can give me would be great.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > just has an error out.
    Details please - what error(s) do you see.

    > limit = n / 2;
    Could be better if you used sqrt(n)

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I can tell you that [code]main(void)l/code] is invalid and should return an integer.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by swgh
    I can tell you that [code]main(void)l/code] is invalid and should return an integer.

    There's nothing wrong with int main(void) at all, which is what the code already has. It does return an int.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    what happens is I get the ms dos window, but it doesn't prompt me to do anything and says press any key to continue and then it closes.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Re-compile your program? You should be prompted for n at least.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    I am posting the current version of this program. I have changed a few things, but the bottom line is that everyone who has seen it says that it should work, but it doesn't. I doesn't prompt me for anything, it just has an error, a window error i believe. I am actually beginning to believe that it is microsoft visual and not my program.

    Code:
    in primes.h
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int	is_prime(int n);
    
    
    
    in main.c
    
    #include "primes.h"
    
    void main(void)
    {
    	{
    	int		n;
    
    	printf("%s" "PRIMES WILL BE PRINTED\n\n"		
    		"How many primes do you want to display?  ");
    	scanf("%d", &n);
    	is_prime(n);
    	}
    }
    
    
    in is_prime.c
    #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;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I don't know about everybody else, but I'm fed up of asking and waiting for something more specific than "an error"

    > void main(void)
    Backward step number 1, you were right with int main

    > printf("%s" "PRIMES WILL BE PRINTED\n\n"
    Still going backwards, where the hell did that %s come from?
    Not that you supply any parameters to the function.
    "this" "is" "a string" is just "this is a string" as far as the compiler is concerned.

    > is_prime(n);
    More regression - you call a function, and throw away the answer.
    Of course you get no output.
    What's more, you lost the loop as well.

    > I am actually beginning to believe that it is microsoft visual and not my program.
    Of course - there's you with mere days of inexperience vs. millions of hours of development time and a vast user base.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    And put the code tags around each of the code segments, not around all the code segments. You should have 3 code blocks.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime factorisation program
    By szpengchao in forum C Programming
    Replies: 4
    Last Post: 04-07-2009, 04:30 PM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. C Program for Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 12
    Last Post: 02-19-2003, 04:55 AM
  4. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM