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