I'm pretty much done with my code which calculates all primes between 2 and 1000. However, I need make a code which scans my prime array and prints whether its a prime or not.
For those who dont understand what I want: After calculating all primes between 2 and 1000, I want the user to give a random number which then tells the user whether its a prime or not. With this code below, it doesnt work for some reason... can someone tell me what I did wrong?
#include <stdio.h>
#include <stdbool.h>
int main () {
int durch [1000] = {0};
for (int i = 2; i <= 1000; i++)
{
if (!durch[i-2])
{
for (int j = 2*i; j <= 1000; j+= i)
{
durch[j-2] = 1;
}
}
}
int primzahlen [1000];
int anzahl = 0;
int n;
for (int i = 2; i <= 1000; i++)
{
if (!durch [i-2])
{
primzahlen[anzahl] = i;
anzahl++;
// printf ("%i : %i\n", anzahl, i);
scanf ("%i\n", &n);
if (n = primzahlen[anzahl]){
printf ("Yes it is a prime!\n");
break;
}
else {
printf ("No it isnt a prime!\n");
break;
}
}
}
return 0;
}
I really cant figure that part out.. I feel like I miss something? Can somebody help me out please?