循环

1、

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int x;
 6 
 7     int ret = 0;
 8 
 9     scanf_s("%d", &x);
10     int t = x;
11     
12     while (x>1)
13     {
14         x = x / 2;
15         ret++;
16     }
17     
18     printf("log2 of %d is %d.", t, ret);
19 
20     return 0;
21 }

2、猜数字

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 int main()
 6 {
 7     /*  
 8     
 9         time()返回 time_t 可能是64位整数。
10         srand()想要一个 unsigned int 这可能是一个32位整数。
11         
12     */
13     srand((unsigned)time(0));        //种子
14     int number = rand() % 100 + 1;
15     int count = 0;
16     int a = 0;
17 
18     printf("我已经想好了一个1到100之间的数。
");
19     do
20     {
21         printf("请猜这个1到100之间数:");
22         scanf_s("%d", &a);
23         count++;
24         if (a > number)
25         {
26             printf("你猜的数大了。");
27         }
28         else
29         {
30             if (a < number)
31             {
32                 printf("你猜的数小了。");
33             }
34             
35         }
36 
37     } while (a!=number);
38 
39     printf("太好了,你用了%d次就猜到了答案。
", count);
40 
41     return 0;
42 }

3、平均数

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int number;
 6     int count = 0;
 7     int sum = 0;
 8     scanf_s("%d", &number);
 9 
10     while (number!=-1)
11     {
12         sum = sum + number;
13         count++;
14         scanf_s("%d", &number);
15     }
16 
17     printf("%f", 1.0 * sum / count);
18 }

4、

 1 /* 整数逆序 */
 2 #include<stdio.h>
 3 
 4 int main()
 5 {
 6     int number;
 7     int result = 0;
 8     int x;
 9     scanf_s("%d", &number);
10 
11     do
12     {
13         x = number % 10;
14         result = result * 10 + x;
15         number = number / 10;
16 
17     } while (number!=0);
18 
19     printf("%d", result);
20 
21     return 0;
22 }
原文地址:https://www.cnblogs.com/2018jason/p/10944581.html