Use the exhaustive method to find 1 to 100 prime numbers and show them. Implemented using the while, Do-while, for Loop statement, respectively.
1. With while:
Include<iostream.h>
void Main ()
{int i,j,n,m;
i=2;
while (i<101)
{m=1;n=i/2;j=2;
while (j<=n)
{if (i%j==0)
{m=0;
Breake;
}
j + +;
}
if (m)
cout<<i<< "";
i++;
}
}
2. With Do...while
#include <iostream.h>
void Main ()
{int i,j,n,m;
i=2;
Todo
{m=1;n=i/2;j=2;
Todo
{if (i%j==0)
{m=0;
Breake;
}
j + +;
}while (j<=n);
if (m)
cout<<i<< "";
i++;
}while (i<101);
}
3. For
# include<iostream.h>
void Main ()
{int i,j,n,m;
for (i=2;i<101;i++)
{m=1;
N=I/2;
for (j=2;j<=k;j++)
{if (i%j==0)
{m=0;
Breake;
}
}
if (m)
cout<<i<< "";
}}
4. #include <iostream.h>
#include <math.h>
void Main ()
{
int flag=1;
for (int i=11;i<101;i++)
{
for (int j=2;j<sqrt (i); j + +)
{
if (i%j==0)
{
flag=0;
Break
}
Else
flag=1;
}
if (flag==0)
cout<< "";
Else
cout<<i<<endl;
}
}
It's no problem this time.
Other similar
5.if (m%2!=0)///If the end of the 2 is not prime, the number is relatively large, efficient
{
k= (int) sqrt (m);
for (n=2;n<=k;n++)//To determine whether a prime number
{
if (m%n==0) break;
}
if (n==k)//Not output
{
count++;
cout<<m<< "";
}