The programme should ask user how many primes does he wants and print out the counts and primes.
My code is:
Code:
#include <stdio.h>
#include<stdlib.h>
int is_prime(int n);
************************************************** ******************************
Code:
#include "primes.h"
int main(void)
{
int n, r, i = 1, primes, cnt = 0, how_many;
printf("PRIMES WILL BE PRINTED. \n\n");
printf("How many do you want to see? ");
scanf("%d", &how_many);
for (n = how_many; n > 0; --n){
i++;
r = is_prime(i);
if(n > 0)
if(r == 1){
primes = i;
++cnt;
printf("%8d%10d\n",cnt, primes);
}
}
return 0;
}
************************************************** ******************************
Code:
#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;
}
************************************************** ***************
for how_many = 9, I get:
1 2
2 3
3 5
4 7
************************************************** *******************
Can any one help me correct this code?
Many thanks.