Prime number function

I've looked through the archived questions and have found no help. Can someone please tell me why my function fails to return any prime numbers?
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
30
31
32
33
#include <iostream>
using namespace std;
bool prime (int);

void main ()
{
	int n, value;
	while (true)
	{
		cout << "Enter n (n >= 2): "; cin >> n;
		cout << endl << "Prime numbers less than or equal to " << n << ":" << endl;
		
		for (value = 2; value <= n; value++)
		{
			if (prime (value))
				cout << value << " ";
		}
	cout << endl << endl;
	}
}

bool prime (int value)
{
	int i;
	for (int i = 2; sqrt (double (value)); i++)
	{
		if (value % i == 0)
		{
			return false;
		}
	}
	return true;
}
At a glance, this line looks wrong:
 
for (int i = 2; sqrt (double (value)); i++)


I would try this:
 
for (int i = 2; i <= sqrt (double (value)); i++)
Last edited on
Yep, that's it. Thanks!
Topic archived. No new replies allowed.