Example 5.1
Assuming that China's total population this year is 1.3 billion, if it increases by 2% per year, the annual population in 10 years from now will be calculated
#include<stdio.h> #include<math.h> int main() { int n = 13, year; double number, rate = 0.02; for (year = 1; year <= 10; year++) { number = n * pow((1 + rate), year); printf("%2d Years later, the number is:%.2f Hundred million\n", year, number); } return 0; }
[example 5.2] calculate 1-4 / 1 + 7 / 1-10 / 1 + 13 / 1-16 / 1 + Until the absolute value of a term is less than 10 to the minus 6 power
#include<stdio.h> #include<math.h> int main() { double sum, item, flag, denominator; sum = 0; item = 1; flag = 1; denominator = 1; while (fabs(item) >= 1e-6) { sum = sum + item; flag = -flag; denominator = denominator + 3; item = flag / denominator; } printf("sum=%f\n", sum); return 0; }
5.2} using the for statement to implement the loop structure
5.2. Basic syntax of 1 for statement
General form of for statement:
For (expression 1; expression 2; expression 3)
Loop body statement;
1. First calculate expression 1;
2. Judge expression 2. If its value is true (not 0), execute the loop body statement, and then execute step 3; If the value is false (0), end the cycle and go to step 5.
3. Calculate expression 3.
4. Return to step 2 to continue;
5. After the loop ends, continue to execute the next statement of the for statement;
[example 5.3] enter a positive integer n to find the value of sum
[example 5.4] enter a positive integer n and find n!
#include<stdio.h> int main(void) { int i, n; double factorial; printf("input n Value of:"); scanf_s("%d", &n); factorial = 1; for (i = 1; i <= n; i++) factorial = factorial * i; printf("%d!=%.0f\n", n, factorial); return 0; }
2. Omit expression 1 from the general form of the for statement
The format is as follows:
for (; expression 2, expression 3)
Loop statement;
Note: when expression 1 is omitted, the initial value of the cyclic variable can be placed before for. Note that the first ";" cannot be omitted at this time.
Circular statement of example 5.3:
1.for(i=1;i<=n;i++)
2.sum=sum+i;
Equivalent to:
1.i=1;
2.for(;i<=n;i++)
3. sum=sum+i;
3. Omit expression 2 from the general form of the for statement
The format is as follows:
for (expression 1; expression 3)
Loop body statement;
4. Omit expression 3 from the general form of the for statement
for (expression 1; expression 2;)
Loop body statement;
Circular statement of example 5.4:
1.for(i=1;i<=n;i++)
2. factorial=factorial*i;
Equivalent to:
1.for(i=1;i<=n;)
2.{ factorial=factorial*i;
3. i++;
4.}
5. The general form of the for statement expression 1 and expression 3 can also be comma expressions
Program section of example 5.3:
1.sum=0;
2.for(i=1,i<=n;i++)
3. sum=sum+i;
Equivalent to:
for(sum=0,i=1;i<=n;i++)
sum=sum+i;
The expression sum=0 and i=1 are comma expressions.
6. The general form of the for statement. As long as the value of expression 2 is not 0, the loop body is executed
For example:
1.for(;(ch=getchar())!='\n';)
2. printf("%c",ch)
7. In the general form of the for statement, the circular statement can be omitted
Circular statement of example 5.3:
1.for(i=1;i<=n;i++)
2. sum=sum+i;
Equivalent to:
for(i=1;i<=n;sum=sum+i,i++);
5.2.2 for recycling example
[example 5.5] enter an integer n to calculate 1-1 / 4 + 1 / 7-1 / 10 + 1 / 13-1 / 16 + Sum of the first n items of
#include<stdio.h> int main() { int n, i, denominator, flag; float sum, item; printf("input n Value of:"); scanf_s("%d", &n); flag = 1; denominator = 1; sum = 0; for (i = 1; i <= n; i++) { item = flag * 1.0 / denominator; sum = sum + item; flag = -flag; denominator = denominator + 3; } printf("Sum=%.2f\n", sum); return 0; }
[example 5.6] input 10 numbers and output the maximum number
[example 5.7] output the number of all daffodils
#include<stdio.h> int main() { int number, a, b, c; for (number = 100; number <= 999; number++) { a = number / 100; b = number % 100 / 10; c = number % 10; if (number == a * a * a + b * b * b + c * c * c) printf("%5d", number); } }
[example 5.8] input a positive integer from the keyboard to judge whether the number is complete
#include<stdio.h> int main() { int number, sum, i; printf("Please enter a positive integer:"); scanf_s("%d", &number); sum = 0; for (i = 1; i <= number - 1; i++) if (number % i == 0) sum = sum + 1; if (number == sum) printf("%d It's perfect\n", number); else printf("%d Not perfect\n", number); return 0; }
[example 5.9] count the number of uppercase English letters, lowercase English letters, numeric characters and other characters in several characters input from the keyboard
#include<stdio.h> int main() { int upper, lower, digit, i, other; char ch; upper = lower = digit = other = 0; printf("Enter 10 characters:"); for (i = 1; i <= 10; i++) { ch = getchar(); if (ch >= 'a' && ch <= 'z') lower++; else if (ch >= 'A' && ch <= 'Z') upper++; else if (ch >= '0' && ch <= '9') digit++; else other++; } printf("Lowercase letters%d Two, capital letters%d Number, number%d Characters, other characters%d individual\n", lower, upper, digit, other); return 0; }
[5.10] arbitrarily input a line of lowercase letters, convert them into uppercase letters and output
#include<stdio.h> int main() { int i; char ch; for (i = 1; (ch = getchar()) != '\n'; i++) putchar(ch - 32); return 0; }
[example 5.11] input three numbers from the keyboard, combine them into an integer number and output
#include<stdio.h> int main() { int i; char ch; for (; (ch = getchar()) != '\n'; ) putchar(ch - 32); return 0; }
[5.12] input a positive integer from the keyboard to judge whether it is a prime number
#include<stdio.h> int main() { int n = 0, i; char ch; printf("Enter 3 numbers:"); for (i = 1; i <= 3; i++) { scanf_s("%c", &ch); n = n * 10 + ch - '0'; } printf("%d\n", n); return 0; }
5.3} using the while statement to implement the loop structure
5.3. 1. Basic syntax of while statement
The general form of while statement is as follows:
while (expression)
Circulatory body;
The execution flow of the while statement is shown in Figure 5.3
Step 1: calculate the value of the expression. If the value of the expression is true, execute step 2; If the value of the expression is false, go to step 4.
Step 2: execute the loop body statement.
Step 3: return to step 1.
Step 4: end the loop and execute the next statement of the while statement.
5.3. 2. while recycling example
[example 5.13] calculate sum
#include<stdio.h> int main() { int i, sum; i = 1, sum = 0; while (i <= 100) { sum = sum + i; i = i + 1; } printf("sum=%d", sum); return 0; }
[5.14] enter a positive integer n to calculate n
#include<stdio.h> int main() { int i; long n, fact; i = 2, fact = 1; printf("Please enter n Value of:"); scanf_s("%ld", &n); while (i <= n) { fact = fact * i; i = i + 1; } printf("%ld!=%ld\n", n, fact); return 0; }
5.15] input a string of characters from the keyboard, and count the number of numeric characters, alphabetic characters and other characters in the input characters respectively
#include<stdio.h> int main() { int digit, letter, other; char ch; digit = letter = other = 0; printf("Please enter a character:"); while ((ch = getchar()) != '\n') if ((ch >= '0') && (ch <= '9')) digit++; else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) letter++; else other++; printf("mathematics%d Number of letters%d One, others%d individual\n", digit, letter, other); return 0; }
5.4 using do while statement to realize loop structure
5.4. 1. Basic usage of do while statement
do
Circulatory body
while (expression);
The execution flow of the do while statement is shown in Figure 5.4
1. Execute loop body statement;
2. Calculate the value of the expression. If the value of the expression is true, return to step 1; If the value of the expression is' false ', proceed to step 3;
3. End the cycle
5.4. 2. Do while recycling example
[example 5.16] calculate sum using do while statement=Value of n
#include<stdio.h> int main() { int i, sum; i = 1, sum = 0; do { sum = sum + i; i = i + 1; } while (i <= 100); printf("sum=%d\n", sum); return 0; }
[example 5.17] find the maximum common divisor and minimum common multiple of two natural numbers
#include<stdio.h> int main() { int a, b, r, n, m; printf("Please enter two integers:"); scanf_s("%d%d",& a, &b); m = a, n = b; do { r = a% b; a = b; b = r; } while (r != 0); printf("%d and%d The greatest common divisor of is:%d\n", m, n, a); printf("The least common multiple is:%d", m * n / a); return 0; }
[example 5.18] enter an integer and count the number of digits
#include<stdio.h> int main() { long n, m; int count = 0; printf("Please enter an integer:"); scanf_s("%ld", &n); m = n; if (n < 0)n = -n; do { n = n / 10; count++; } while (n != 0); printf("integer%ld have%d digit\n", m, count); return 0; }
5.5 jump statements that change the loop structure
5.5. 1. break statement
break ends the entire loop
[example 5.19] analyze the running results of the following programs
#include<stdio.h> int main() { int i = 5; do { if (i % 3 == 1) if (i % 5 == 2) { printf("%d", i); break; } i++; } while (i != 0); return 0; }
[example 5.20] input a positive integer from the keyboard to judge whether it is a prime number
#include<stdio.h> #include<math.h> int main() { int n, m, i; printf("Please enter a positive integer:"); scanf_s("%d", &n); m = sqrt(n); for (i = 2; i <= m; i++) if (n % i == 0) break; if (i > m) printf("%d It's a prime!\n", n); else printf("%d Not prime!\n", n); return 0; }
[example 5.21] input the scores of a group of students from the keyboard, calculate the average score, and count the number of failed scores
#include<stdio.h> int main() { int num, n; float scote, total = 0; num = 0, n = 0; while (1) { printf("Please enter a score#%d(0~100):", n + 1); scanf_s("%f", &scote); if (scote < 0) break; if (scote < 60) num++; total = total + scote; n++; } printf("The average score is:%.2f.\n", total / n); printf("Those who fail are:%d.\n", num); return 0; }
5.5. 2. continue statement
Function: end this cycle and start the next cycle
[example 5.22] the number that can be divided by 7 from 1 to 100 is output on the screen in the form of 5 per line
#include<stdio.h> int main() { int i, n = 1; for (i = 1; i <= 100; i++) { if (i % 7 != 0) continue; printf("%4d", i); if (n++ % 5 == 0)printf("\n"); } return 0; }
[example 5.23] analyze the running results of the following program
#include<stdio.h> int main() { int n, s = 0; n = 1; while (n < 10) { s = s + n; if (s > 5) break; if (n % 2 == 1) continue; n++; } printf("s=%d,n=%d\n", s , n); return 0; }
5.5. 3. goto statement
There are four loop control statements: for loop statement, while loop statement, do while loop statement and goto loop
[example 5.24] calculate sum using goto statement=Value of n
#include<stdio.h> int main() { int i, sum; i = 1, sum = 0; loop: if (i <= 100) { sum = sum + i; i = i + 1; goto loop; } printf("sum=%d\n", sum); return 0; }
5.6 loop nesting
When a loop contains another complete loop structure, it is called nested loop or double loop. In general, up to three cycles are used.
Nested for statement in 1for statement
Nested while statement in 2for statement
3 nested while statements in while statements
4do -- nested for statement in while statement
5do -- nested do -- while statement in while statement
6nested do in while statement -- while statement
When using loop nesting, pay attention to the following points:
1. The outer loop shall be executed once and the inner loop shall be executed once. That is, the outer loop is executed once, and the inner loop is executed until the conditions are not met; When the external loop is executed once and the internal loop is executed until the conditions are not met, it is the second round; This is repeated until the outer loop conditions are not met, and the whole loop nesting ends.
2 the inner loop body in the loop nested format is not allowed to cross, that is, the outer loop should contain the inner loop.
3 loop nesting is to use break statement and continue in the inner loop body.
[example 5.25] find 1+ 2!+ 3!+...+ 10!
#include<stdio.h> int main() { int i, j; double factorial, s = 0; for (i = 1; i <= 10; i++) { factorial = 1; for (j = 1; j <= i; j++) factorial = factorial * j; s = s + factorial; } printf("1!+2!+3!+...+10!=%.0f\n", s); return 0; }
[example 5.26] output multiplication formula table
#include<stdio.h> int main() { int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= 1; j++) printf("%d*%d=%d\t", j, i, j * i); } return 0; }
[example 5.27] find prime numbers within 100. It is required to output 10 per line.
#include<stdio.h> #include<math.h> int main() { int i, n, k, count = 0; n = 2; while (n < 100) { k = sqrt(n); for (i = 2; i <= k; i++) if (n % i == 0) break; if (i > k) { printf("%4d", n); if (++count % 10 == 0) printf("\n"); } n++; } return 0; }
[example 5.28] decompose a positive integer between 10 and 20 into prime factors
#include<stdio.h> int main() { int i, n, m; for (m = 10; m <= 20; m++) { n = m, i = 2; printf("%d=", n); do { if (n % i == 0) { printf("%d*", i); n = n / i; } else i++; } while (n != i); printf("%d\n", n); } return 0; }
5.7} typical algorithm examples
5.7. 1. Recursive method
[example 5.29]
#include<stdio.h> int main() { int day, d1, d2; day = 9; d2 = 1; do { d1 = (d2 + 1) * 2; d2 = d1; --day; } while (day > 0); printf("I picked it the first day%d\n", d1); return 0; }
[example 5.30] find sin (x) = x-x ^ 3 / 3+ x^5/5!- x^7/7!+... The myopia value is required to be accurate to the power of 10 minus 6
#include<stdio.h> #include<math.h> #define eps 1e-6 int main() { int n = 1; float x; double fz, fm = 1, sinx; printf("input x Value of:"); scanf_s("%f", &x); fz =x,sinx = x; do { n = n + 1; fz = -fz * x * x; fm = fm * (2 * n - 2) * (2 * n - 1); sinx = sinx + fz / fm; } while (fasb(fz / fm) > eps); printf("sin(%f)=%0.6f\n", x, sinx); printf("sin(%f)=%0.6f\n", x, sin(x)); return 0; }
[example 5.31]
#include<stdio.h> #include<math.h> #define eps 1e-6 int main() { float x1, x0, f, f1; x1 = 1.0; do { x0 = x1; f = ((2 * x0 - 4) * x0 + 3) * x0 - 6; f1 = (6 * x0 - 8) * x0 + 3; x1 = x0 - f / f1; } while (fabs(x1 + -x0) > eps); printf("%6.2f", x1); return 0; }
[example 5.32]
#include<stdio.h> int main() { int men, women, child; for (men = 0; men <= 9; men++) for (women = 0; women <= 12; women++) { child = 36 - men - women; if (men * 4 + women * 3 + child * 0.5 == 36) printf("male:%d female:%d child:%d\n", men, women, child); } return 0; }
Example 5.33] judge how many three digit numbers that are different from each other and have no duplicate numbers can be composed of four numbers 1, 2, 3 and 4? Output these numbers
#include<stdio.h> int main() { int i, j, k, n = 0; for (i = 1; i < 5; i++) for (j = 1; j < 5; j++) for (k = 1; k < 5; k++) if (i != k && 1 != j && j != k) { printf("%d%d%d\t", i, j, k); if (++n % 5 == 0) printf("\n"); } printf("\n share:%d\n", n); return 0; }
5.8: example of loop programming
[example 5.34]
1. Define integer i, j;
2. If I < = 5; Turn to step 3, then turn to step;
3. If I < = 20-i; Go to step 4; Then go to step 5;
4. Output "";
5.j++; Go to step 3;
6. If J < = 2 * I-1; Go to step 7; Then go to step 9;
7. Output "*";
8.j++; Go to step 6;
9. Output \ n;
10.i + +, go to step 2;
9. Conclusion;
#include<stdio.h> int main() { int i, j; for (i = 1; i < 5; i++) { for (j = 1; j <= 20 - i; j++) printf(""); for (j = 1; j <= 2 * i - 1; j++) printf("*"); printf("\n"); } return 0; }
[example 5.35]
1. Define m, n, count;
2.m=rand()%(80-10+1)+10;
3. Enter an integer between 10 and 80;
4. Judge whether it is true, turn to step 5, then turn to step 13;
5.count++;
6. If m==n; Go to step 7; Then go to step 9;
7.m>n&&count<5;
8. Output "sorry! You guessed right! Do it again!";
9.m<n&&count<5;
10. Output "sorry! You guessed big! Do it again!";
11. If count==5; Turn to step 12, then turn to step 4;
12. Output "sorry! n you have no chance!"
13. Conclusion;
#include<stdio.h> #include<stdlib.h> int main() { int m, n, count = 0; m = rand() % (80 - 10 + 1) + 10; printf("Please enter a 10~80 Integer between:"); while(1) { scanf_s("%d", &n); count++; if (m == n) { printf("congratulations! You guessed right, you're great!\n"); break; } else if (m > n && count < 5) printf("I'm sorry! Guess it's small!Once more!"); else if (m < n && count < 5) printf("I'm sorry! Guess how big! Once more!"); if (count == 5) { printf("I'm sorry!You have no chance!\n This number is%d,game over!\n", m); break; } } return 0; }
[example 5.36]
#include<stdio.h> #include<math.h> int main() { int x, i, j = 0, n, k = 0; for (x = 100; x < 1000; x++) { k = sqrt(x); for (i = 2; i <= k; i++) if (x % i == 0) break; if (i > k) { k = x; n = 0; while (k > 0) { n = n * 10 + k % 10; k /= 10; } if (x == n) { printf("%d\t", x); if (++j % 5 == 0)printf("\n"); } } } return 0; }
[example 5.37]
1. Definition x, t;
2. Enter "i|tpower\n";
3. If x < 1000; Go to step 4; Then step 10;
4.t=x*x;
5. If t= 0 Go to step 6; Then go to step 9;
6. If x==t%1000; Go to step 7; Then go to step 8;
7. Output X; x*x;
8.t=t/10;
9.x + +, go to step 3;
10. Conclusion;
#include<stdio.h> int main() { int x, t; printf("i\tpower\n"); for (x = 100; x < 1000; x++) { t = x * x; while (t != 0) { if (x == t % 1000) { printf("%d\t%d\n", x, x * x); break; } else t = t / 10; } } return 0; }