Using the filter method to find the prime number within 100, the use of arrays is required.
#include "stdafx.h"
#include <iostream>
using namespace Std;
int _tmain (int argc, _tchar* argv[])
{
int a[100],i,b[100],j,n=0; Array a[100] To put in the number of judgements, array b[100] to place prime numbers
for (i=0;i<100;i++)//Note here I cannot start from 1 because the first of array A is a[0]
{
a[i]=i+1; The number in a[100] is from 1 to 100.
}
for (i=2;i<=100;i++)//0 and 1 do not judge
{
for (j=2;j<i;j++)
{
if (i%j==0)
{
Break If I can divide any number from 2 to i-1, then I is not a prime, jumping out of the loop
}
if (j==i-1)//If the above conditions have not been established, then j=i-1 after the end of the cycle, I is the prime number
{
B[n]=i;
n++; Put I in order in the array b[100]
}
}
}
for (i=0;i<n;i++)
{
cout<<b[i]<<endl; The number stored in the output array B, in turn
}
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Using the filter method to find the prime number within 100 (array)