# Beginning to learn c language: small exercises of branching and cycling

Keywords: C

1. Print prime numbers between 100-200

Trial division

Idea: the definition of prime number is that the number except 1 and itself cannot be divided, and integers can be written in the form of c=a*b, with a or B < = sqrt(c) (sqrt means square opening). That is to say, if the number before sqrt(c) can be divided, it means that it is not a prime number. If it cannot be divided, the number behind sqrt(c) must not be divided, Then this number is prime.

```#include<stdio.h>
#Include < math. H > / / sqrt library function is used later
int main ()
{
int i=0;
int count =0;//Used as the back for counting
for (i=100;i<=200;i++) //To find a prime number between 100 and 200, first turn it into a number between 100 and 200
{
int j=0;
for (j=2;j<=sqrt(i);j++)//Generate a number less than the square i
{
if (i%j==0)//Here, judge that if the remainder of i divided by j is equal to 0, it means that it is divided by whole, so it is not a prime number that jumps out of the loop
{
break;
}
}
if (j>sqrt(i))// If it is greater than the square i, it means that no number can be divided before the square i, and it will not be divided after that. Then this is a prime number
{
count++;//  Find a count once
printf("%d ",i);
}
}
printf("\n");
printf("count=%d\n",count);//Finally, print out the counting times
return 0;
}```

2. Output 9 * 9 multiplication formula table

Idea: the 9 * 9 formula is

1*1=1

2*1=2  2*2=4

3*1 =3 3*2 =6  3*3 =9

...  ...

9*1=9  9*2 =18 ...

It can be seen that if the columns are from 1 to 9 and the rows are from 1 to 9, they can be generated by the for loop respectively.

```#include<stdio.h>
int main ()
{
int i =0;
for (i=1;i<=9;i++)//Control the first factor for a total of 9 lines
{
int j=0;
for (j=1;j<=i;j++)//Control the second factor
{
printf("%d*%d=%-2d ",i,j,i*j);//%2D is to print two digits, and the right alignment digit is not enough. Replace%-2d left alignment with a space
}
printf("\n");
}
return 0;
}```

3. Judge leap years from 1000 to 2000

Idea: the leap year rule is one leap in four years, no leap in a hundred years, and four hundred years are leap. Therefore, you can traverse the numbers from 1000 to 2000, which is a multiple of 4. At the same time, it is not a multiple of 100, but a leap year, or a multiple of 400 is also a leap year. These two conditions are or. The code is as follows

```#include<stdio.h>
int main ()
{
int year=0;
int count =0;
for (year=1000;year<=2000;year++)
{
if(year%4==0 && year%100!=0) //The judgment is a multiple of 4 and not a multiple of 100
{
printf("%d ",year);
count++;
}
else if (year%400==0)// The second judgment rule determines the multiple of 400
{
printf("%d ",year);
count++;
}
}
printf("\ncount=%d ",count);//Count to see how many leap years there are
return 0;
}```

4. Calculate 1+ 2!+...+ 10!

Idea: first n= N * (n-1) * (n-2) *.. * 2 * 1. Because each factor is continuous and factorial from 1 to 10, it can be described by two-layer cycle. The code is as follows:

The process is: the first time n=1, enter the cycle i=1,ret=1,ret=1*1, which is equivalent to calculating 1!; sum=0+1=1;

For the second time, n=2, enter the cycle i=1,ret=1,ret=1*1, 1 < 2, and then enter the small cycle i=2,ret=1*2, which is equivalent to calculation 2!; sum=1+2;

For the third time, n=3, enter the cycle i=1,ret=1,ret=1*1, 1 < 2, then enter the small cycle i=2,ret=1*2, 2 < 3, and continue to enter the small cycle i=3,ret=2*3; sum=3+6;

.........

```#include <stdio.h>
int main ()
{
int i=0;
int n=0;
int sum=0;
for (n=1;n<=10;n++)// First generate a number from 1 to 10 to find n! Incoming parameters
{
int ret=1;
for (i=1;i<=n;i++)
{
ret =ret*i;//Here we can calculate n! But n can't be too large, or it will overflow. It's OK for this question
}
sum =sum+ret;
}
printf("sum=%d\n",sum);
return 0;
}```

5. Binary search

Idea: for example, find 7 from 1 2 3 4 5 6 7 8 9 10, and the subscripts of each element are 0-9 in turn. The conditions that can be found are clearly explained in the notes, mainly including the conditions that cannot be found:

If you are looking for 3 from 1 2 4 5 6 7 8 9 10 11   The situation is as follows

 Search times Left element subscript (corresponding number) Middle element subscript (corresponding number) Subscript of right element (corresponding number) Size relationship between intermediate element and 3 1st time 0(1) 4(6) 9(11) >   on the left 2nd time 0(1) 1(2) mid-1=3(5) < on the right 3rd time mid+1=1(2) 2(4) 3(5) >   on the left 4th time 1(2) 1(2) mid-1=2(4) < on the right 5th time mid+1=2(4) 1(2) 2(4) < on the right 6th time mid+1=3(5) 2(4) 2(4) >On the left

It can be seen that the subscript of the left element is greater than that of the right element     You'll never find it again.

```#include<stdio.h>
int main ()
{
int arr[]={1,2,3,4,5,6,7,8,9,10};
int sz =sizeof(arr)/sizeof(arr);//Calculate the number of elements
int left=0;//The leftmost element subscript is 0
int k=7;  //If you are looking for 7
int right =sz-1; //The rightmost number subscript is sz-1
while (left<=right)  //This equal sign cannot be less
{
int mid=(left+right)/2;//Subscript of intermediate element
if(arr[mid]>k)  //The subscript of the middle element is greater than the number to be found, indicating that the number is on the left of the middle. The subscript on the left is fixed, and the subscript on the right is 1 less than the middle value
//So take out the right half
{
right=mid-1;
}
else if (arr[mid]<k)//The subscript of the middle element is less than the number to be found, indicating that the number is on the right side of the middle. The subscript on the right side is fixed, and the subscript on the left side is 1 more than the middle value
//So take out the left half
{
left=mid +1;
}
else //If neither of the above is satisfied, it means that it is the intermediate element
{
printf("Yes, the subscript is%d\n",mid);
break;
}
}
if(left>right) //When this situation is found, it means it can't be found
{
printf("can't find\n");
}
return 0;
}```

6. Guessing numbers games

Idea:

1. The number guessing game is nothing more than that the computer randomly gives a number. You guess, but the main problem is how the computer gives numbers?

In fact, the rand function of c language can be used. Although this function can generate random numbers, when you quit the game and play again, the random numbers are still the same. In that way, you can only play the game once, and playing later is equivalent to knowing the answer. Therefore, you need to use the srand function to call the rand function. The main parameter of the srand function is an unsigned integer, There is also a problem here, that is, after you set the number, the number generated by Rand will be the same. Therefore, you need to pass in a random value to the srand function. We can use the timestamp to pass in the random value to make the srand function generate a random value. This timestamp is implemented by the time function. The code is as follows:

```#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#Include < time. H > / / the time function is used below
#Include < stdlib. H > / / srand library function is used
{
printf("*********************\n");
printf("******1.Play games*****\n");
printf("******0.Exit the game*****\n");
printf("*********************\n");
}
void game()
{
int ret =0;
int guess=0;
ret =rand()%100+1;//Because the random number is very large, for the game experience, turn it into a number within 100, find the remainder, and then + 1 to get a number from 0 to 100
while(1)
{
printf("Please guess the number is between 0 and 100\n");
scanf("%d",&guess); //%d cannot be followed by line breaks and spaces
if(ret >guess)
{
printf("Guess it's small\n");
}
else if (ret <guess)
{
printf("Guess big\n");
}
else
{
printf("Congratulations, you guessed right. That's awesome\n");
break;
}
}
}
int main()
{
int input=0;
//Void srand (unsigned int seed) the value generated by srand if the seed is written as an unsigned integer
//It will not change all the time, so you need to change the seed all the time, so you can use the timestamp to realize the constant change of the seed
//Use the timestamp to set the timestamp. The timestamp is the time of the current computer minus the starting time of the computer (0:00:0 on January 1, 1970)
srand((unsigned int)time(NULL));//Call with srand before using rand
do
{
scanf("%d",&input);//%d cannot be followed by a space, otherwise you have to enter it twice
switch(input)  //Branch statement case 1 play game case 0 exit game
{
case 1:
game();
break;
case 0:
printf("Exit the game\n");
break;
default:
printf("Input error, please select a number between 1 and 0 again");
break;
}
}
while (input);//When the input is false, i.e. 0, the game ends
return 0;
}```

Finally, the process of playing the game is as follows:   You can see that the two are different numbers. Press 0 to exit the game.

end

The first time I wrote a blog, my knowledge is also very basic, but I summarized it with my heart. I hope you guys don't spray it   thank!

Posted by PHP'er on Wed, 10 Nov 2021 16:03:56 -0800