prime number problem

hi
can someone help me?

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.


Last edited on
Don't double post.
Hi!

My simple solution is the following (visual studio):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdafx.h"
#include <iostream>

using namespace 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;
}



Please use the Insert code for your code.

thanks ;)
Topic archived. No new replies allowed.