实验2 c语言分支与循环基础应用编程

发布时间 2023-10-20 12:55:34作者: Meng36

实验1

实验1的源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 5
 5 #define N1 374
 6 #define N2 465
 7 int main(){
 8  int number;
 9  int i;
10  
11  srand( time(0));//以当前系统时间作为随机种子
12  
13  for(i=0;i<N;++i){
14   number = rand()%(N2 - N1 + 1) + N1;//随机得到一个数字
15   printf("202383290376%04d\n",number);
16  }
17  
18  system("pause");
19  return 0;
20 }

实验1的运行结果

实验2

实验2的源代码

 1 #include<stdio.h>
 2 int main (){
 3  char colour;
 4  
 5  printf("请输入字符r表示red,g表示green,y表示yellow:");
 6  while(scanf("%c",&colour) != EOF){
 7      getchar();
 8  switch(colour){
 9   case 'r':printf("stop\n");break;
10   case 'g':printf("go go go\n");break;
11   case 'y':printf("wait a minute\n");break;
12   default:printf("something must be wrong\n");
13  }
14 }
15     return 0;
16 }

 

实验2 运行结果

 

实验3

实验3的源代码

 1 #include<stdio.h>
 2 #include<time.h>
 3 #include<stdlib.h>
 4 
 5 int main(){
 6  srand(time(0));
 7  int number = rand()%30+1;
 8  int a;
 9  int i=1;
10  printf("猜猜2023年11月哪一天会是你的lucky day");
11  printf("开始咯,你有三次机会,猜吧(1~30):");
12   
13  for(i=1;i<=3;i++){
14   scanf("%d",&a);
15   if(a>number){
16    printf("你猜的日期晚了,你的lucky day已经过啦\n");
17   }else if (a<number){
18    printf("你猜的日期早了,你的lucky day还没到呢\n");
19   }if(a==number){
20    printf("哇,猜中了\n");
21   }
22  
23  }printf("次数用完啦,偷偷告诉你:11月,你的lucky day是%d\n",number);
24  
25  return 0;
26 }

实验3 运行结果截图

 实验4

源代码

 1 #include<stdio.h>
 2 int main(){
 3  int a,n,m;
 4  double s;
 5  
 6  
 7  while(scanf("%d%d",&n,&a)!= EOF){
 8      int i;
 9      int m = 0;
10      s=0;
11      for(i=1;i<=n;i++){
12           m=m*10+a;
13           s += 1.0*i/m;
14           
15         
16           
17 
18    }
19      printf("n = %d,a = %d,s = %lf\n",n,a,s);
20  }
21  
22  
23  
24  return 0;
25 }

运行结果

 

 

 

实验5

实验5的源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main(){
 5     int i,j;
 6     for(i=1;i<=10;i++){
 7         for(j=1;j<=i;j++){
 8             printf("%dx%d=%-4d",j,i,i*j);
 9         
10         
11         }
12         printf("\n");
13     
14     
15     }
16 
17 
18     system("pause");
19     return 0;
20 }

 

实验5运行结果

 实验6

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     int n;
 7     int i,l,f,k,j;
 8 
 9     scanf("%d",&n);
10 
11     for(i=1;i<=n;i++)
12     {
13         int m;
14         for(m=1;m<=i-1;m++)
15         {
16            printf("\t");
17         }
18         
19         for(j = 1;j<=2*n-1-2*(i-1);j++)
20         {
21             printf(" o \t");
22 
23         }
24         printf("\n");
25 
26         for(k=1;k<=i-1;k++)
27         {
28             printf("\t");
29         
30         }
31         for(f=1;f<=2*n-1-2*(i-1);f++)
32         {
33             printf("<H>\t");
34         
35         }
36         printf("\n");
37 
38         for(k=1;k<=i-1;k++)
39         {
40             printf("\t");
41         }
42         for(l = 1;l<=2*n-1-2*(i-1);l++)
43         {
44             printf("I I\t");
45         }
46         printf("\n");
47 
48 }
49     system("pause");
50     return 0;
51 
52 
53 }

 

运行结果