# Thread: Hi, Can anyone help me? print prime numbers.

1. ## Hi, Can anyone help me? print prime numbers.

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.

2. Welcome to the forum, Kelai.

Sorry to say, not many will want to study code that is not surrounded by code tags so it stays looking like code, instead of html text and all squished to the left margin.

Change it by highlighting your code, and clicking on the # icon. You have to be in the advanced reply/edit window to see the # icon.

3. Looks like you're counting down no matter if it found a prime or not.
You want to decrement n only during a prime.

4. Originally Posted by nonoob
Looks like you're counting down no matter if it found a prime or not.
You want to decrement n only during a prime.
It's working fine now.
Many many many thanks.