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:
#include<iostream>
usingnamespace 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.