5.2 Implementing loops with a while statement
while (expression) statement
The loop body statement is executed whenever the loop condition expression is true (that is, the given condition is set).
The feature of the while loop is that the conditional expression is judged first, and then the loop body statement is executed.
Example 5.1 seeking 1+2+3+...+100.
#include <stdio.h> #include <stdlib.h>int main () { int i=1,sum=0; while (i<=100) { sum=sum+i; i++; } printf ("sum=%d\n", sum); return 0;}
5.3 Using Do...while statements to implement loops
Do...while statement execution process is: First execute the loop body, and then check whether the condition is set up, if a re-execution loop body. This is different from the while statement.
The Do...while statement is characterized by executing the loop body unconditionally and then judging whether the loop condition is true or not.
Use Do...while to ask for the sum above.
#include <stdio.h> #include <stdlib.h>int main () { int i=1,sum=0; Do { sum=sum+i; i++; } while (i<=100); printf ("sum=%d\n", sum); return 0;}
While and Do...while, when the 1th time value of the expression after the while is true, the two loops get the same result, otherwise the results are different.
5.4 Looping with a for statement
For (the loop variable assigns the initial value; cyclic condition; loop variable increment)
Statement
When you enter a computer from a terminal keyboard, you press the ENTER key to send a batch of data together into the memory buffer.
5.5 Loops of nesting
A loop body also contains another complete loop structure, called a nested loop. Loops can also be nested within an inline loop, which is a multilayer loop.
5.6 Comparison of several cycles
While loops, do...while loops, and for loops, you can use the break statement to jump out of the loop and end the loop with the continue statement.
5.7 Changing the state of the loop execution
Terminates the loop prematurely with the break statement.
The effect of break is to cause the process to jump outside the loop body and then execute the statement below the loop body.
The break statement can only be used in a loop statement and a switch statement, not in a separate use.
End this cycle in advance with the Continue statement
Continue ends this cycle prematurely, skipping the following statement that has not yet been executed in the loop body, moving to the end of the loop body, then executing expression 3 in the For statement, and then making the next decision whether to execute the loop.
Example 5.5 requires the number of outputs between 100~200 that cannot be divisible by 3.
#include <stdio.h> #include <stdlib.h>int main () { int n; for (n=100;n<=200;n++) { if (n%3==0) continue; printf ("%d", n); } printf ("\ n"); return 0;}
The continue statement simply ends the loop, and the break statement ends the entire loop.
5.8 Examples of cyclic procedures
Example 5.8 The first 40 numbers of the Fibonacci series . This series has the following characteristics: 1th, 22 numbers are the sum of the first two digits of the 3rd number.
This is an interesting classical math problem: there are a pair of rabbits, starting from the 3rd month after birth, each month is a pair of rabbits. The rabbit grows to a pair of rabbits every month after the 3rd month. If all rabbits are not dead, ask how many rabbits are in each month.
#include <stdio.h> #include <stdlib.h>int main () { int f1=1,f2=1,f3; int i; printf ("%12d\n%12d\n", f1,f2); for (i=1;i<=38;i++) { f3=f1+f2; printf ("%12d\n", F3); F1=F2; f2=f3; } return 0;}
Program improvements:
#include <stdio.h> #include <stdlib.h>int main () { int f1=1,f2=1; int i; for (i=1;i<=20;i++) { printf ("%12d%12d", f1,f2); if (i%2==0) printf ("\ n"); F1=F1+F2; f2=f2+f1; } return 0;}
Example 5.9 Enter an integer greater than 3 N to determine whether it is a prime (prime, also known as prime number).
#include <stdio.h> #include <stdlib.h>int main () { int n,i; printf ("Please enter a integer number,n=?"); scanf ("%d", &n); for (i=2;i<=n-1;i++) if (n%i==0) break; if (i<n) printf ("%d is not a prime number.\n", n); else printf ("%d is a prime number.\n", n); return 0;}
Program improvements:
#include <stdio.h> #include <stdlib.h>int main () { int n,i,k; printf ("Please enter a integer number:n=?"); scanf ("%d", &n); K=SQRT (n); for (i=2;i<=k;i++) if (n%i==0) break; if (i<=k) printf ("%d is not a prime number.\n", n); else printf ("%d is a prime number.\n", n); return 0;}
Example 5.10 asks for the total number of primes between 100-200.
#include <stdio.h> #include <stdlib.h> #include <math.h>int main () { int n,k,i,m=0; for (n=101;n<=200;n=n+2) { k=sqrt (n); for (i=2;i<=k;i++) if (n%i==0) break; if (i>=k+1) { printf ("%d", n); m=m+1; } if (m%10==0) printf ("\ n"); } printf ("\ n"); return 0;}
Example 5.11 Password. In order to keep the message confidential, it is often converted into a password according to certain laws, and the receiving reporter then translates it into the original text according to the rules of the agreement.
For example, you can turn a message into a password as follows:
Turns the letter A into a letter E,a E, which becomes the 4th letter thereafter. W becomes a,x into b,y into C.
Entering a line of characters from the keyboard requires the corresponding password to be output.
#include <stdio.h> #include <stdlib.h> #include <math.h>int main () { char C; C=getchar (); while (c!= ' \ n ') { if ((c>= ' a ' &&c<= ' z ') | | (c>= ' A ' &&c<= ' Z ')) { if (c>= ' W ' &&c<= ' Z ' | | C>= ' W ' &&c<= ' z ') c=c-22; else c=c+4; } printf ("%c", c); C=getchar (); } printf ("\ n"); return 0;}
Program improvements:
#include <stdio.h> #include <stdlib.h> #include <math.h>int main () { char C; while ((C=getchar ())! = ' \ n ') { if ((c>= ' A ' &&c<= ' Z ') | | (c>= ' A ' &&c<= ' Z ')) { c=c+4; if (c>= ' z ' &&c<= ' z ' +4| | C> ' z ') c=c-26; } printf ("%c", c); } printf ("\ n"); return 0;}
3. Enter two positive integers m and N to find their greatest common divisor and least common multiple.
#include <stdio.h> #include <stdlib.h> #include <math.h>int main () { int p,r,n,m,temp; printf ("Please enter two positive integers n,m:"); scanf ("%d,%d", &n,&m); if (n<m) { temp=n; n=m; m=temp; } P=n*m; while (m!=0) { r=n%m; n=m; m=r; } printf ("Their greatest common divisor is:%d\n", n); printf ("Their minimum number of conventions:%d\n", p/n); return 0;}
4. Enter a line of characters to count the number of letters, spaces, numbers, and other characters in each of them.
#include <stdio.h> #include <stdlib.h> #include <math.h>int main () { char C; int letters=0,space=0,digit=0,other=0; printf ("Please enter a line of characters: \ n"); while ((C=getchar ())! = ' \ n ') { if (c>= ' a ' &&c<= ' Z ' | | c>= ' A ' &&c<= ' Z ') letters++; else if (c== ') space++; else if (c>= ' 0 ' &&c<= ' 9 ') digit++; else other++; } printf ("Number of letters:%d\n spaces:%d\n Number:%d\n Other characters:%d\n", letters,space,digit,other); return 0;}
6. Ask for 1!+2!+3!+4!+...+20!.
#include <stdio.h> #include <stdlib.h> #include <math.h>int main () { double s=0,t=1; int n; for (n=1;n<=20;n++) { t=t*n; s=s+t; } printf ("1!+2!+3!+4!+...+20!=%22.15e\n", s); return 0;}
8. Output all of the " daffodils ", the so-called "daffodils number" refers to a 3-digit number, whose numbers are cubic and equal to the number itself. For example, 153 is a narcissus number because of 153=1+125+27.
#include <stdio.h> #include <stdlib.h> #include <math.h>int main () { int i,j,k,n; printf ("Parcissus numbers is"); for (n=100;n<1000;n++) { i=n/100; j=n/10-i*10; k=n%10; if (n==i*i*i+j*j*j+k*k*k) printf ("%d\n", n); } printf ("\ n"); return 0;}
9. If a number is exactly equal to the sum of its factors, this number is called the "end number", for example, the factor of 6 is three-to-one, and 6=1+2+3, so 6 is the "Finish". The program finds all the finished numbers within 1000 and outputs its factors in the following format:
6 its factors is 1 2 3
Method 1:
#include <stdio.h> #include <stdlib.h> #define M 1000int Main () {int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10; int i,a,n,s; for (a=2;a<=m;a++) {n=0; S=a; for (i=1;i<a;i++) if (a%i==0) {n++; S=s-i; Switch (n) {case 1:k1=i;break; Case 2:k2=i;break; Case 3:k3=i;break; Case 4:k4=i;break; Case 5:k5=i;break; Case 6:k6=i;break; Case 7:k7=i;break; Case 8:k8=i;break; Case 9:k9=i;break; Case 10:k10=i;break; }} if (s==0) {printf ("%d,its factors is", a); if (n>1) printf ("%d,%d", K1,K2); if (n>2) printf (",%d", K3); if (n>3) printf (",%d", K4); if (n>4) printf (",%d", K5); if (n>5) printf (",%d", K6); if (n>6) printf (",%d", K7); if (n>7) printf (",%d", K8); if (n>8) printf (",%d", K9); if (n>9) printf (",%d", K10); printf ("\ n"); }} return 0;}
Method 2:
#include <stdio.h> #include <stdlib.h> #include <math.h>int main () { int m,s,i; for (m=2;m<1000;m++) { s=0; for (i=1;i<m;i++) if ((m%i) ==0) s=s+i; if (s==m) { printf ("%d,its factors is", m); for (i=1;i<m;i++) if (m%i==0) printf ("%d\t", I); printf ("\ n");} } return 0;}
C Language Programming-5th chapter design of cyclic structure