C++ Prime Number Examples




C++ Prime Number Examples

#include <iostream>
#include <iomanip>      
 
using namespace std;
 
int main()
{
    int num,i,count,n;
    cout << "Enter max range: ";
    cin >> n;
    for(num = 1;num<=n;num++){
        count = 0;
        for(i=2;i<=num/2;i++){
            if(num%i==0){
                count++;
                break;
            }
        }
 
        if(count==0 && num!= 1)
            cout << num << setw(3);
    }
    return 0;
}

Output

Enter max range: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89