Enter all the primes between 100 and 200.
(1) through a two-tier for loop
#include <stdio.h>
int main ()
{
int i;
Int J;
for (i=100;i<=200;i++)
{
for (j=2;j<=i-1;j++)
{
if (i%j==0)
Break
}
if (j==i)
printf ("%d", I);
}
return 0;
}
Optimization:
#include <stdio.h>
int main ()
{
int i;
Int J;
for (i=101;i<=200;i=i+2)
{
for (j=2;j<=i/2;j++)
{
if (i%j==0)
Break
}
if (J>=I/2)
printf ("%d", I);
}
return 0;
}
(2) by comparing the number of records
#include <stdio.h>
int main ()
{
int i,j;
int count=0;
for (i=100;i<=200;i++)
{
count=0; Each time count needs to start at 0
for (j=2;j<=i-1;j++)
{
if (i%j==0)
Break
Else
count++;
}
if (count==i-2)//Record Count comparison
printf ("%d", I);
}
return 0;
}
(3) Boolean type
#include <stdio.h>
int main ()
{
int i,j;
BOOL Flag;
for (i=100;i<=200;i++)
{
Flag = true;
for (j=2;j<=i-1;j++)
{
if (i%j==0)
{
Flag=false;
Break
}
}
if (flag)
printf ("%d", I);
}
return 0;
}
Note: Applying a Boolean type is not possible in VC6.0. Can be implemented in Microsoft Visual Studio 2008.
Solution of Prime number (C language)