Reputation: 45
Hi All,
I'm creating a prime-number checker as a part of a project - I'm using a For-Loop to iterate for each number and another to iterate each number below it to work out if it's prime. However, neither of the loops work. I've checked it against other working For-Loops i've created and with friends and neither have helped at all (sorry friends). Any help given will be appreciated and i'll forever hold you in high praise :)
As just stated, i've checked against other pieces and with my friends. i've also tried running the code within the loops separately and the code does indeed work.
#include <stdio.h>
//int FinalPrimeArray[sizeof(Primes)];
//int FinalNotPrimesArray[sizeof(NotPrimes)];
int main()
{
//int Pos = 0;
int Pos2 = 0;
int i = 3;
int k;
int divider = 2;
int productint;
float productintasfloat;
float productfloat;
int Primes[100];
//int NotPrimes[100];
//Test Block (start)
float i2 = i;
float divider2 = divider;
productfloat = i2 / divider2;
printf("float type is: %f \n", productfloat);
productint = i / divider;
productintasfloat = productint;
printf("integer type is: %d \n", productint);
if(productintasfloat == productfloat)
{
printf("yes");
}
//Test Block (end)
printf("Calculating all prime numbers in 3-100");
for(i = 3; i == 100; i++)
{
printf("1");
k = i - 1;
for(divider = 2; divider == k; divider++)
{
printf("2");
float i2 = i;
float divider2 = divider;
productfloat = i2 / divider2;
printf("float type is: %f \n", productfloat);
productint = i / divider;
productintasfloat = productint;
printf("integer type is: %d \n", productint);
if(productintasfloat == productfloat)
{
Primes[Pos2] = i;
Pos2++;
}
}
}
return 0;
}
I would expect it to iterate 97 times (for the first loop), each main iteration iterating 'i - 1' times in turn. As surely made quite clear by now... this is not the case. When ran I get no error-messages, it prints the two lines of code from the text block and the "3-100" message. Then nothing, no error message or further output. However, the code does terminate (Stop symbol, greys out - usually coloured and clickable when code is running)
Any help would be much appreciated and if no one can help... well i guess i'll have to find another way around it. While I would prefer any fixes to remain close to my intended method (I'm not trying to be a ChoosingBeggar) any fix at all would be much appreciated :)
-> Budding Developer
If i've missed any useful information and you want to help - don't be afraid to ask.
Upvotes: 0
Views: 68
Reputation: 3418
I think you have mixed up how for loops work
The for loop will run as long as the condition is true until it becomes false. What you believe is the for loop will run as long as the condition is false until it becomes true.
So for(i = 3; i == 100; i++)
should be for (i = 3; i < 100; i++)
This now says keep incrimenting i
while it is less than 100. Your previoius loop said increment i
while it is 100. Which it never is
Upvotes: 2