#include <iostream>
#include <iomanip>
using namespace std;
//---------------------
void clear(int []);
void print_array(int [] , int );
void main(void)
{
int n , i = 2 , j , value , a[101];
cout << "input n = ? (2~100)" << endl ; cin >> n ;
clear(a); //清除陣列
while(i < n )
{
if(a[i] == 0)
{
j = 2 ;
value = i * j++ ; // i 的倍數設為 1
while(value < n)
{
a[value] = 1 ;
value = i * j++;
}
}
i++;
}
print_array( a , n ) ;
cin.get();cin.get();
}
//--------------------
void clear(int a[])
{
int i ;
for( i = 0 ; i < 101 ; i++ )
a[i] = 0 ;
}
//--------------------
void print_array(int a[] , int n )
{
int i ;
for(i = 2 ; i < n ; i++)
if(a[i] == 0 )cout << " " << i << " ";
cout << endl ;
}
執行結果 :