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