12345678910111213141516
#include <iostream> using namespace std; int main () { int i, j; for(i = 2; i<100; i++) { for(j = 2; j <= (i/j); j++){ if(!(i%j)) break; // if factor found, not prime } if(j > (i/j)) cout << i << " is prime\n"; } return 0; }
12345678910111213141516171819202122232425262728293031323334353637383940
#include <iostream> using namespace std; int main () { int i, j; cout << "find prime numbers in [70,99]\n=============================\n\n" ; for(i = 70; i<100; i++) { cout << "\nouter loop: i == " << i << '\n' << " enter inner loop, test divisibility by " ; for(j = 2; j <= (i/j); j++){ cout << j << ", " ; if( !(i%j) ) // if factor found, not prime { cout << "factor found (" << i << '/' << j << "), not prime, break" ; break ; } } // *** end inner loop *** cout << "\ninner loop ended with j == " << j ; if( j > (i/j) ) { cout << ", " << j << " > (" << i << '/' << j << ")\n" << "********* ergo " << i << " is prime *********\n"; } else { cout << ", " << j << " <= (" << i << '/' << j << ")\n" << "ergo " << i << " is not prime\n"; } } // *** end outer loop *** }