How would I change this loop to print prime number in reverse… starting from the biggest one first
int main(){
bool prime;
for( int i=3; i<=10000; i++){
prime = true;
for(int n=2; n<=i-1; n++){
if( i%n == 0){
prime = false;
}
}
if(prime){
cout << i << " ";
}
}
return 0;
}
You can reverse the for loop as follows:
That being said – you can also simplify this. You only need to check until you reach the square root of the number. Also make sure that, when you find that a number isn’t prime, you break out immediately: