# Thread: Need Some Coaching, Display 100 Prime Numbers

1. ## Need Some Coaching, Display 100 Prime Numbers

I've been working at this for awile and I need some help. I can print the first 100 non-prime numbers but not the first 100. What am I missing? This nested loop business is killing me. Here is what I have:

Code:
```#include<stdio.h>

int main (void)
{
int num, test;
int prime = 0;
int not_prime = 0;

for (num = 2; num < 100; num++)
{

for (test = 2; test < (num - 1); test++)

{
if (num % test == 0)
{
printf ("%d\n", num);
break;
}
}
//printf ("%d\n", num);

}
}```

2. I can print the first 100 non-prime numbers but not the first 100
Huh?

3. Not the first 100 prime.

4. Do you want to print out non-prime numbers? I don't know what your requirements are. You never stated them clearly.

Regardless, you need to work out on paper how you'd do this as if you were a computer. Flowchart it , then write the code.

Throwing loops together haphazardly is a recipe for disaster.

5. I want to print the first 100 prime numbers. I am able to do the all of the non primes from 0 to 100 but cannot figure out how to get the primes. This is what I think I need to do.

Make a loop that goes from 2 to 100.
Make another loop that will test each value by using modulus 2 to the number -1
If any 0's are found, the number is not prime
If no zeroes are found the number is prime

I can't figure out how to catch the primes. Actually I have caught them a couple times but they print for every trip through the loop. I can;t figure out how to just catch them one at a time.

6. See bold
Originally Posted by dolfaniss

Make a loop that goes from 2 to 100.
Make another loop that will test each value by using modulus 2 to the number -1
If any 0's are found, the number is not prime <---set a flag variable (say, prime_found ) and break from the loop either directly, or by the test at the top of the loop
If no zeroes are found the number is prime

I can't figure out how to catch the primes. Actually I have caught them a couple times but they print for every trip through the loop. I can;t figure out how to just catch them one at a time.

7. Something like this (not tested)
Code:
```    prime_found = FALSE;
for (test = 2; test < (num - 1); test++) {
if (num % test == 0) {
prime_found = TRUE;
break;
}
}
if (prime_found) {
printf ("%d\n", num);
}```

8. Thanks Mike, I will play around and see what I can come up with.