# whats wrong with this?

#include <stdio.h>

int main()
{
int i;
int a[]={1,2,3,4,5,6,7,8};
for(i=0;a[i]/i==1;a[i]/1==1,i++);
printf("prime numbers are:");
return 0;
}

answered Jul 12 by (7,160 points)
edited Jul 12 by Peter Minarik

# What is a prime number?

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. ;)

# Problems with the code

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.

# Suggested solution

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!