Finding primes

I need to write a program that gets an integer 'n' as input and outputs all of the odd primes less than or equal to 'n'. This is what I currently have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;

int main(){
  int num;
  bool prime;

  cout << "Please enter a positive integer" << endl;
  cin >> num;

  for(int i = 3; i <= num; i++){
    prime = true;
    for(int n = 2; n <= i - 1; n++){
      if(i % n == 0){
        prime = false;
      }
    }
    if(prime){
      cout << i << " is prime" << endl;
    }
  }

  return 0;
}


Now whilst this works, it runs slowly when calculating with large numbers. What I'd like to do is use some while loops and make it run more efficiently. Any help anyone could offer would be greatly appreciated.
Use this function to check if it is prime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int IS_prime(double num)
{
	int isprime = 0;
	for(int i = 2; i <= sqrt(num); i += 2)
	{
		if(i % 2 == 0)
			i++;

		if((int(num)% i) == 0)
		{
			isprime = 1;
			break;
		}
	}

	return isprime;                                              
}
Topic archived. No new replies allowed.