C language elementary loop and branch statement practice

insert image description here

1. Calculate the factorial of n

code show as below:

#include<stdio.h>
int main()
{
    
    
    int n,i,s=1;
    printf("输入一个整数计算它的阶乘:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    
    
        s*=i;
    }
    printf("%d!=%d\n",n,s);
    return 0;
}

2. Calculate 1!+2!+3!+...+10!

code show as below:

#include <stdio.h>
int main()
{
    
    
    int n=0;
    int i=0;
    int s = 1;
    int sum = 0;
    for (n = 1; n <= 10; n++)
    {
    
    
        s *= n; 
        sum += s;
    }
    printf("sum=%d\n",sum);
    return 0;
}

3. Ninety-nine multiplication table

code show as below:

#include <stdio.h>
int main()
{
    
    
	int i = 0;
	//控制行数
	for(i=1; i<=9; i++)
	{
    
    
		//打印每一行内容,每行有i个表达式
		int j = 0;
		for(j=1; j<=i; j++)
		{
    
    
			printf("%d*%d=%2d ", i, j, i*j);
		}
		printf("\n");
	}
	return 0;
}

4. Find the maximum value of 10 numbers

code show as below:

int main()
{
    
    
	int arr[10] = {
    
    0};
	int i = 0;
	int max = 0;

	for(i=0; i<10; i++)
	{
    
    
		scanf("%d", &arr[i]);
	}
	//
	max = arr[0];
	for(i=1; i<10; i++)
	{
    
    
		if(arr[i]>max)
			max = arr[i];
	}
	printf("max = %d\n", max);
	return 0;
}

5. Calculate the value of 1/1-1/2+1/3-1/4+1/5 ... + 1/99 - 1/100, and print out the result

code show as below:

#include <stdio.h>
int  main()
{
    
    
	int i = 0;
	double sum = 0.0;
	int flag = 1;
	for(i=1; i<=100; i++)
	{
    
    
		sum += flag*1.0/i;
		flag = -flag;
	}
	printf("%lf\n", sum);
	return 0;
}

6. Write a program to count how many numbers 9 appear in all integers from 1 to 100

code show as below:

#include <stdio.h>
int main()
{
    
    
	int i = 0;
	int count = 0;


	for(i=1; i<=100; i++)
	{
    
    
		if(i%10==9)
			count++;
		if(i/10==9)
			count++;
	}
	printf("%d\n", count);
	return 0;
}

7. Binary search

code show as below:

#include <stdio.h>

int main()
{
    
    
	int arr[] = {
    
    1,2,3,4,5,6,7,8,9,10};
	int key = 3;
	int left = 0;
	int right = sizeof(arr)/sizeof(arr[0]); // right位置的数据取不到

	while(left<right) // right位置没有数据,此处不需要添加=
	{
    
    
		int mid = left+(right-left)/2;
		if(arr[mid]>key) // key小于中间位置数据,说明key可能在左半侧,需要改变右边界
		{
    
    
			right = mid; // right位置的数据取不到,因此right=mid,不需要减1
		}
		else if(arr[mid]<key)// key大于中间位置数据,说明key可能在右半侧,需要改变左边界
		{
    
    
			left = mid+1; // left位置的数据可以取到,因此left=mid+1
		}
		else
		{
    
    
			printf("找到了,下标是:%d\n", mid);
      break;
		}
	}
   
	if(left>=right)
		printf("找不到\n");
	return 0;
}

8. Write code to demonstrate that multiple characters move from both ends and converge toward the middle.

code show as below:

#include <stdio.h>
int main()
{
    
    
    char arr1[] = "welcome world...";
    char arr2[] = "################";
    int left = 0;
    int right = strlen(arr1) - 1;
    printf("%s\n", arr2);
    //while循环实现
    //while (left <= right)
    //{
    
    
    //    Sleep(1000);
    //    arr2[left] = arr1[left];
    //    arr2[right] = arr1[right];
    //    left++;
    //    right--;
    //    printf("%s\n", arr2);
    //}
    //for循环实现
    for (left = 0, right = strlen(arr1) - 1;
        left <= right;
        left++, right--)
    {
    
    
        Sleep(1000);
        arr2[left] = arr1[left];
        arr2[right] = arr1[right];
        printf("%s\n", arr2);
    }
    return 0;
}

9. Write code to implement, simulate the user login scenario, and only log in three times. (Only allow to enter the password three times. If the password is correct, it will prompt that the login is successful. If the input is wrong three times, the program will exit)

code show as below:

int main()
{
    
    
    char psw[10] = "" ;
    int i = 0;
    int j = 0;
    for (i = 0; i < 3 ; ++i)
   {
    
    
        printf( "please input:");
        scanf("%s", psw);
        if (strcmp(psw, "password" ) == 0)
            break;
   }
    if (i == 3)
        printf("exit\n");
    else
        printf( "log in\n");
}

10. Guess the number game

code show as below:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
    
    
 	printf("**********************************\n");
 	printf("*********** 1.play     **********\n");
 	printf("*********** 0.exit     **********\n");
 	printf("**********************************\n");
}
//RAND_MAX--rand函数能返回随机数的最大值。
void game()
{
    
    
 	int random_num = rand()%100+1;
 	int input = 0;
 	while(1)
 	{
    
    
 		printf("请输入猜的数字>:");
 		scanf("%d", &input);
 		if(input > random_num)
 		{
    
    
 			printf("猜大了\n");
 		}
 		else if(input < random_num)
 		{
    
    
 			printf("猜小了\n");
 		}
 		else
 		{
    
    
 			printf("恭喜你,猜对了\n");
 			break;
 		}
 	}
}
int main()
{
    
    
 	int input = 0;
 	srand((unsigned)time(NULL));
 	do
 	{
    
    
 		menu();
 		printf("请选择>:");
 		scanf("%d", &input);
 		switch(input)
 		{
    
    
 			case 1:
 				game();
				break;
 			case 0:
 				break;
 			default:
 				printf("选择错误,请重新输入!\n");
 				break;
 		}
 	}while(input);
 return 0;
}

11. Shut down spoof applet

The goto
code is as follows:

#include <stdio.h>
int main()
{
    
    
    char input[10] = {
    
    0};
    system("shutdown -s -t 60");
    again:
    printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
    scanf("%s", input);
    if(0 == strcmp(input, "我是猪"))
   {
    
    
        system("shutdown -a");
   }
    else
   {
    
    
        goto again;
   }
    return 0;
}

The loop writing
code is as follows:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    
    
    char input[10] = {
    
    0};
    system("shutdown -s -t 60");
    while(1)
   {
    
    
        printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
        scanf("%s", input);
        if(0 == strcmp(input, "我是猪"))
       {
    
    
            system("shutdown -a");
            break;
       }
   }
    return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/kingxzq/article/details/130680766