my programme is suppost to print in the screen all the prime numbers smaller than the one i enter in the command line. but there are numbers missing or appering wrong ( if I enter 11 are appearing 5, 7 and 9 when should be 2, 3, 5, 7, 11).
Note:a prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself.To check whether a given number is prime, rather than generate a list of primes given a number n, one divides n by all numbers m less than or equal to the square root of that number. If any of the divisions come out as an integer, then the original number is not a prime. Otherwise, it is a prime.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
bool isPrime( int n )
{
bool prime = true;
for ( int i = 2; (i <= n / 2) && (prime == true); i++ )
if (n % i == 0)prime = false;
return prime;
}
int _tmain(int argc, _TCHAR* argv[])
{
int number;
cin >> number;
for ( int i = 2; i <= number; i++ )
if (isPrime(i))
cout << i << " is prime" << endl;
cin.get();
return 0;
}