Beginner C ++, intended to learn footprint with blog record. Write a program seeking two prime numbers, minor changes.
. 1 #include <the iostream> 2 #include <math.h> . 3 the using namespace STD; . 4 int main () { . 5 int m, n-, K, I, J, H; . 6 n-= 0 ; // n-should not be defined within the outer loop, or can not achieve the effect of line feed . 7 CIN >> m; . 8 // start I k is defined in vitro cycle, leading to failure and will change as the value of k after each inner loop, the outer loop to be repeated defined . 9 for (I = . 1 ; I <= m; I ++ ) 10 { . 11 K = . 1 ; // may be provided Boolean effect, k value the same as the Boolean 12 is H =sqrt (I); 13 is for (J = 2 ; J <= H; J ++ ) 14 { 15 IF (% I J == 0 ) 16 { . 17 K = 0 ; 18 is BREAK ; . 19 } 20 is } // BREAK Application the position of the inner loop, comprising a first inner loop if (k) statement, leading to print something less than 21 is IF (K) 22 is { 23 is COUT I << << " " ; 24 n-+ = . 1 ; 25 if (n % 10 == 0) 26 cout << endl; 27 } 28 29 } 30 system("pause"); 31 return 0; 32 }
1 #include <iostream> 2 #include <math.h> 3 using namespace std; 4 int minum, maxnum; 5 int i, j, k; 6 int main() { 7 int count = 0; 8 cout << "请输入两个数:" << endl; 9 cin >> minum >> maxnum; 10 for(i=minum;i<=maxnum;i++){ 11 k = sqrt(i); 12 for (j = 2; j <= k; j++) { 13 is IF (i% J == 0 ) 14 BREAK ; 15 } 16 IF (J> = K + . 1 ) // determination condition is whether or not i is a prime number . 17 { 18 is COUT << i << " " ; . 19 COUNT ++ ; 20 is IF (COUNT% 10 == 0 ) 21 is COUT << endl; 22 is } 23 is } 24 System ( " PAUSE"); 25 return 0; 26 }