# 1, Circulation

## 1.1 cycle

Now I have a problem: the program reads in a positive integer with less than 4 bits (including 4 bits), and then outputs the number of bits of this integer. If input 352, output 3.

People's way: you can see it with your eyes. We can see that 352 is a three digit number at a glance.
The way of computer: what the computer is not good at is to see the results at a glance, so an appropriate method is to judge the range of numbers to determine its digits. Since 352 ∈ , 352 is a three digit number.

Therefore, we write the code as follows. Through the cascaded if to judge whether the input number is greater than the maximum three digits, the maximum two digits and the maximum one digit in turn, so as to obtain the number of digits of the number.

```#include <stdio.h>
int main()
{
int x;
int n=1;

scanf("%d", &x);
if (x > 999)
{
n=4;
}
else if (x > 99)
{
n = 3;
}
else if (x > 9)
{
n = 2;
}
else
{
n = 1;
}

printf("%d\n", n);
return 0;
}
```

We can see that the results are correct. But this code also has limitations:

What if you enter a positive integer in any range? If it's 5 digits, 6 digits, 7 digits... Then we have to increase the number of if all the time. How is this the end? We know that if you see the number 352, you can know it is three digits at a glance. But if people see the number 123812843267518273618275317, can they see how many digits it is at a glance? The answer is obviously not, then we will count and count all the way.

About counting, how do people do it:

The process of counting people is as follows: count from left to right, cross out one number at a time, and add one digit. For example, if you enter 352, we first cross out the first 3, keep the following 52, and add 1.

What does the computer do:

How should the computer realize this function? To complete the above operation, we can make 352% 100 - > 52. At this time, successfully cross out 3 and keep 52, and then repeat the above operation. So, for the very large number just now, to cross out the first number, you need to
123812843267518273618273612675317%
100000000000000000000000000000000->
23812843267518273618273612675317
The question is how to get that
100000000000000000000000000000000？

Idea:
It can be seen that it is unrealistic to draw from left to right. It is impossible to know which number should be modeled. If you change, start from the right until there is no number to draw, and count in the process, so you can get the correct result.

123812843267518273618273612675317 / 10->12381284326751827361827361267531
12381284326751827361827361267531 / 10->1238128432675182736182736126753
1238128432675182736182736126753 / 10->123812843267518273618273612675
...

When we write the code, we can see that we need to always judge whether the number after delimitation is 0. If it is not 0, we need to continue. It can be seen that this matter is still endless.

```int x;
int n = 0;

scanf("%d", &x);

n++;
x /= 10;
if < x>0)
{
n++;
x/=10;
if (x > 0)
{
n++;
x/=10;
if ...
}
}

printf("%d\n", n);
```

Then we may need this thing: while. When x is greater than 0, we need to do this all the time.

```#include <stdio.h>
int main()
{
int x;
int n=0;
scanf("%d", &x);
n++;
x /= 10;
while (x > 0)
{
n++;
x /= 10;
}

printf("%d\n", n);
return 0;
}
```

We can see that the results are correct. ## 1.2 while loop

Just looking at the syntax, while is very similar to if. Just change the if in front of the judgment condition to while. The difference is that the operation if in the bracket is executed only once, while the while is executed repeatedly.

```//if:
if (x > 0)
{
x /= 10;
n++;
}

//while
if (x > 0)
{
x /= 10;
n++;
}
```

The flow chart of while can be shown in the following figure: What is repeated in parentheses is called a loop body. There must be opportunities to change the conditions in the circulatory system, otherwise it will always be unable to get out of the circulatory system. As in the following example, since x is greater than 0 and the value of X will not change, n will be added all the time.

```int x = 10;
int n = 0;
while (x > 0)
{
n++;
}
printf("%d", n);
```

In the above section, we wrote a code to judge the number of integer digits. The first two lines of code in while are the same as the code in the loop body. Can we put the external code in?

```n++;
x /= 10;
while (x > 0)
{
n++;
x /= 10;
}
```

Let's try.

```#include <stdio.h>
int main()
{
int x;
int n=0;
scanf("%d", &x);
while (x > 0)
{
n++;
x /= 10;
}

printf("%d\n", n);
return 0;
}
```

When we enter 0, the result is 0, which is obviously wrong. Therefore, this program needs some special codes to judge the case when the input is 0, that is, the operation in the loop body must be performed first.

## 1.3 do while cycle

Let's look at the algorithm flow of the above code for judging integer digits (including the input of 0):

1. User input x;
2. Initialize n to 0;
3. x = x / 10, remove bits;
4,n ++；
6. Otherwise n is the result.

Do we have a better structure to do things first and then judge? This is our do while loop. When entering the loop, do not check, but check whether the conditions of the loop are met after executing the code of the loop body. If satisfied, continue the next cycle; if not, end the cycle. The syntax of do while is as follows. Be careful not to forget the semicolon after while.

do
{
< loop body statement >
}While (< cycle condition >);

The flow chart is shown in the figure below: A do while loop is similar to a while loop. The difference is that do while judges the conditions at the end of the execution of the loop body. That is, the loop will execute at least once anyway, and then judge the condition. The same as the while loop is that the loop is executed when the conditions are met, and ends when the conditions are not met. Therefore, the code for judging the number of integer digits can be written as follows.

```#include <stdio.h>
int main()
{
int x;
int n=0;
scanf("%d", &x);
do
{
n++;
x /= 10;
}while (x > 0);

printf("%d\n", n);
return 0;
}
```

## quiz

1. When the conditions of the while loop are met, the loop continues, and when the conditions of the do while loop are met, the loop ends.

2. What is the value of variable i after the execution of the following code fragment?

```int i =10;
while ( i>0 ) {
i /=2;
}
```

3. What is the value of variable i after the execution of the following code fragment?

```int i = 1;
do {
i += 5；
} while (i<17);
```

# 2, Cyclic application

## 2.1 cycle calculation

Title 1: find the power of a number to 2. (set the input number to be an integer power of 2)

```#include <stdio.h>
int main()
{
int x=64;
int ret = 0;
int t = x;  //Use a variable to save x, because the original x needs to be printed later, and the value of later x will change
while (x > 1)
{
ret++;
x /= 2;
}

printf("log2 of %d is %d\n", t, ret);
return 0;
}
```

Topic 2: let's take another example. I have a piece of code below.

```#include <stdio.h>

int main()
{
int count = 100;
while ( count>= 0 ) {
count--;
printf("%d ", count);
}
printf("launch\n");

return 0;
}
```

Looking at this code, we have the following small problems:

① How many times does this loop need to be executed?
② When the loop stops, does it output the last 0?
③ What is the value of count after the loop ends?

Small routine: if you want to simulate a large number of cycles, you can simulate a small number of cycles and then make inference. Let's set count to 3 first. It can be seen that the loop has been executed 4 times, the last 0 is output, and the value of count after the loop is - 1. ## 2.2 guessing game

Title: now we want to play a guessing game. Let the computer think of a number, and then let the user guess. Each time the user enters a number, he will tell it whether it is large or small until the user guesses it correctly. Finally, he will tell the user how many times it guesses.

Seeing this problem, we can know that the loop needs to be used because it needs to be repeated for users to guess. The core focus is on the condition of the loop, especially the condition of loop termination. We use words to describe our idea of designing code:

1. The computer thinks of a number randomly and records it in the variable number;
2. A variable count responsible for counting times is initialized to 0;
3. Let the user enter a number a;
4. count increment (plus one);
5. Judge the size relationship between a and number. If a is large, output "large"; If a is small, output "small";
6. If a and number are not equal (large or small), the program returns to step 3;
7. Otherwise, the program outputs "guess" and the number of times, and then ends.

We write the corresponding code

```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
/*
You need to use stdlib.h and time.h libraries.
Each call to rand() yields a random integer.
srand(time(0)) is added to make the random integer more realistic.
The random number% n will get an integer of ∈ [0, n-1]. Based on this, a number between 1 and 100 is generated.
*/
srand(time(0));
int number = rand()%100+1;
int count = 0;
int a = 0;
printf("I've figured out a number between 1 and 100.");
//It is better to use do while, so it will be executed once
do {
printf("Please guess the number between 1 and 100:");
scanf("%d", &a);
if ( a > number ) {
} else if ( a < number ) {
}
count ++;
} while (a != number);
printf("Great, you used it%d I guessed the answer once.\n", count);

return 0;
}
```

Let's test it and we can see that the result is correct. In fact, you can guess the integer within 100 up to 7 times. We will analyze the specific reasons later. ## 2.3 average

Title: let the user input a series of positive integers, and finally input - 1 to indicate the end of input. Then the program calculates the average of these numbers and outputs the number and average of input numbers.

Seeing this problem, we think:

① You need a variable that records the integer you read
② What's the average? Just add it to an accumulated variable every time you read a number, and take it after all the data is read
It can remove the number of reads
③ You need a variable to record the result of the accumulation
④ A variable is required to record the number of reads

The algorithm flow of the program is shown in the following figure: We write the corresponding code as follows:

```#include <stdio.h>

int main()
{
int sum = 0;
int count = 0;
int number;

scanf("%d", &number);
while ( number != -1 ) {
sum += number;
count ++;
scanf("%d", &number);
}

printf("The average is %f.\n", 1.0 * sum / count);

return 0;
}
```

We can see that the results are correct. ## 2.4 integer inversion

Title: enter a positive integer and output the number in reverse order. For example, input 48102 and output 20184.

There are two situations for this problem. ① For some positive integers ending in 0, such as 700, output 7. ② For some positive integers ending in 0, such as 700, 007 is output

1. For the first case, our code is as follows:

```#include <stdio.h>

int main()
{
int x;
scanf("%d", &x);
int digit;
int ret = 0;

while ( x> 0 ) {
digit = x%10;
ret = ret*10 + digit;
printf("x=%d,digit=%d,ret=%d\n", x, digit, ret);
x /= 10;
}
printf("%d", ret);

return 0;
}
```

The test shows that the results are correct. 2. For the second case, our code is as follows:

```#include <stdio.h>

int main()
{
int x;
scanf("%d", &x);
int digit;
int ret = 0;

while ( x> 0 ) {
digit = x%10;
printf("%d", digit);
ret = ret*10 + digit;
x /= 10;
}

return 0;
}
```

The test shows that the results are correct. ## quiz

1. Which of the following operations can obtain the lowest decimal digit from the variable x
A. x / 10
B. x % 10
C. x * 10
D. 10 / x