1. ## 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. > 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. I can tell you that [code]main(void)l/code] is invalid and should return an integer.

4. 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. 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. Re-compile your program? You should be prompted for n at least.

7. 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. 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. And put the code tags around each of the code segments, not around all the code segments. You should have 3 code blocks.