This is my code for calculating primes. Basically I take a number divide by all the previous primes that i calculated to check if it is a prime or not. I was not getting correct answer. I kept changing the code to figure out the mistake. I found that i value is not initializing to 0 whenever it enters the loop. Instead i is 1,2,3. I cannot understand why this is happening. I'm using visual studio c. Please explain why is this happening ???
#include<stdio.h>
#include<conio.h>
int main()
{
int primes[10000];
int i,j,k;
int primecheck;
primes[0]=2;
k=1;
for(j=3;;j+=2)
{
primecheck = 1;
i=0;
for(i=0;i<k;i++);
{
printf("%d\n%d\n%d\n",i,j,j%primes[i]);// this helped me figure
// out the problem
if(j%primes[i] == 0)
{
primecheck = 0;
printf (" %d, %d\n",j,primes[i]);
}
printf("%d\n",k);
}
if (primecheck == 1)
{
primes[k]=j;
printf("%d prime is %d\n",k+1,primes[k]);
k++;
if(k == 5)
{
break;
}
}
}
printf("%d",primes[k-1]);
getch();
}