0 votes

First, you have to understand what prime numbers are: https://en.wikipedia.org/wiki/Prime_number

- "is a natural number greater than 1 that is not a product of two smaller natural numbers"
- "a different but equivalent definition of the primes: they are the numbers with exactly two positive divisors, 1 and the number itself."

So knowing this, you can try to write your code again. ;)

From programming perspective, the code looks funny. You have an array called *a* on which you would like to iterate through and check if the elements are primes or not. A typical loop would do the body as many times as many items are in the array.

I would start off with something like this:

#include <stdio.h> #define NUMBER_COUNT 10 int isPrime(int number) { // TODO: Implement isPrime(): return TRUE if `number` can be divided by 1 and itself only. return 0; } void main() { int numbers[NUMBER_COUNT] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; printf("Prime numbers: "); for (int i = 0; i < NUMBER_COUNT; i++) { if (isPrime(numbers[i])) printf("%d, ", numbers[i]); } }

Now you can write the body of the *isPrime()* method and test if your code works correctly. -- *Of course, I have written my version of it, but I don't want to ruin the fun for you, so go ahead, and give it a try. ;)*

When you're done with it, you can try to optimize it further.

Also, feel free to post your solution if you want some one to have a quick review of it.

Good luck!

...