Console Program to list prime numbers

I would like to know your input regarding this program.




#include <iostream>

using namespace std;

/*
This program computes and displays all the prime numbers between 2 and the number entered by the user.

Test Cases:

Input Output
8 2 3 5 7
4 2 3
10 2 3 5 7
25 2 3 5 7 11 13 17 19 23
15 2 3 5 7 11 13

*/

int main(int argc, char * args[])

{
int n;
cout << "Please enter a positive integer: " << endl;
cin >> n;

if (cin.fail())
{
cout << n << " is a bad integer." << endl;
}

cout << "The following numbers are all prime numbers between 2 and " << n << ": ";

for (int i = 2; i <= n; ++i)
{
//if i is prime, then print it.

bool iIsPrime = true;
for (int k = 2; k < i; ++k)
{
if (i % k == 0)
{
// i is not prime.
iIsPrime = false;
}
}
if (iIsPrime)
{
cout << i << " ";
}
}
cout << endl;
}
Topic archived. No new replies allowed.