C language classic programming 282 examples 01

premise:

Because I used to have a lot of exposure to concepts, now I will operate it in practice.

01 Hello world

Speaking of the first program, I have to start with Hello world!

#include<stdio.h>

main()
{
    
    
	printf("hello word!"); //;分号要注意!
	printf("\n");
 } 

02 Adding two values-a complete C language program

#include<stdio.h>

main()
{
    
    
	int a, b, sum = 0;//sum记得赋初值
	a = 10;
	b = 20;
	sum = a + b;//方向不要弄反
	
	printf(" sum = %d", sum);//
	printf("\n");
 } 

03 It is precious and constant, why bother from three to five shifts to sleep; the most useless, I am afraid that it will be exposed to the cold for ten days a day!

#include<stdio.h>

main()
{
    
    
	printf(" 贵有恒,何必三更起五更睡;最无益,只怕一日曝十日寒! ");
	printf("\n");
 } 

04 Square perimeter

code:

	h = a * 4;  

Do not write daily forms:

	h = 4a;  
#include<stdio.h>

main()
{
    
    
	int a, h;
	a = 4;
	h = a * 4;  
	 
	printf(" h = %d ", h);
	printf("\n");
 } 

05 output a square

#include<stdio.h>

main()
{
    
    
	printf("* * * * *\n");//'\n'为转义符
    printf("*       *\n");
    printf("*       *\n");
	printf("*       *\n");   
	printf("* * * * *\n");	

 } 
Character constant
General character constant Special character constant
General character constant Such as'A', '8','a'
Special character constant Start with '\', followed by one or more characters, each escape character has a specific meaning

Insert picture description here

Note:
(i) The escape character can only be lowercase letters, and each escape character can only be regarded as one character.
(2) The vertical tab character'\v' and the form feed character'\f' have no effect on the screen, and will affect the printer to perform corresponding operations
. In C, the escape character means unprintable characters.

06 Output a triangle

#include<stdio.h>

main()
{
    
    
	printf("    *    \n");
    printf(" *     *  \n");
	printf("* * * * *\n");	

 } 

07 Summation Procedure

#include<stdio.h>

main()
{
    
    
    int a, b, sum = 0; 		/*声明变量*/
    a = 123;			 	/*为变量赋初值*/
    b = 789;				/*为变量赋初值*/
    sum = a + b;			/*求和运算*/
     
	printf("  sum = %d\n", sum );/*输出结果*/
 } 

08 Seek 10!

#include<stdio.h>

main()
{
    
    
    int i = 2, n = 10;   /*定义变量i,n为整型,i=2n=10*/
    float fac = 1;		 /*定义变量fac为单精度型,赋初值fac=1*/
    if(n == 0 || n == 1) /*当n=0 /1 时输出阶乘1*/
    {
    
    
    	printf("  factorial is 1 \n" );
    	return 0;
	}
   
    while(i <= n)		/*当数值大于等于i时执行循环体*/
    {
    
    
    	fac = fac * i;  /*实现求阶乘过程*/
    	i ++;			/*变量i自加*/
	}
	printf(" factorial of %d is: %.2f.\n",  n, fac );
   

 } 

注意: Define the final result of the factorial as single-precision/double-precision type, if it is defined as integer type, it is easy to appear 溢出phenomenon.

//me:
#include<stdio.h>

main()
{
    
    
    int i;
    float sum = 1;
   
   for(i = 1; i <= 10 ; i ++)
   {
    
    
   	sum *= i;
   }
     
	printf("  sum = %f\n", sum );
   

 } 

09 3 numbers are sorted from small to large

#include<stdio.h>

main()
{
    
    
    int a, b, c;
    scanf("%d %d %d\n", &a, &b, &c);
    
    int temp;
    if(a > b)
    {
    
    
    	temp = a;
    	a = b;
    	b = temp;
	}
	if(a > c)
    {
    
    
    	temp = a;
    	a = c;
    	c = temp;
	}
	if(b > c)
    {
    
    
    	temp = b;
    	b = c;
    	c = temp;
	}
    
	printf("  %d  %d  %d\n",  a, b, c );
   

 } 

① There are 3 forms of if statement:

  • if (expression) statement

  • Insert picture description here

  • Insert picture description here
    ②, else cannot be used alone, it must be a part of if statement
    ③, if statement can be nested.
    ④ One or more embedded operation statements can be included after if and else.

010 Monkey Eats Peach

Monkeys eat peaches: The monkeys picked a few peaches on the first day. The monkeys ate more than half of the peaches on the first day. The next day they ate more than half of the remaining peaches. After that, they ate one more peach from the previous day every morning, on the 10th. I want to eat only one in the morning. How many peaches did the monkey pick on the first day?
Suppose there are x, and the rest the next day: x=x/2-1;

//官方
#include<stdio.h>

main()
{
    
    
    int day, x1, x2;
    day = 9;
	x2 = 1;
    
  while(day > 0)
  {
    
    
  	x1 = (x2 + 1) * 2;
	x2 = x1;
	day--;   
  }
	
	printf("  %d  \n",  x1 );
   

 } 
//me
#include<stdio.h>

main()
{
    
    
    int a = 1, i;		//设置原先桃只有1个;i为次数
    
    for(i = 0 ; i < 9; i++)//因为i从0开始算,就要<9,不能等于
    {
    
    
    	a= (a +1) * 2;
	}
	
	printf("  %d  \n",  a );
   
 } 

011 Buy Apple

Buy apples for yin and yang, an apple is 0.8 yuan, buy 2 on the first day, and twice as much on the first day the next day, until the number of apples purchased does not exceed 100, what is the average daily cost of yin and yang?

#include<stdio.h>

main()
{
    
    
    int n = 2, day = 0,;
	float money = 0, ave;
    
    while(n < 100)			//苹果个数不超过100,故while 中的表达式n小于100
    {
    
    
    	  money += n * 0.8;		//将每天花的钱数累加求和
    	  day++;				//天数+1
    	  n *= 2; 				//苹果每天购买的数目变化。
	}
    
	ave = money / day ;
   
	printf("  %.06f  \n",  ave );
   
 } 
//me
#include<stdio.h>

main()
{
    
    
    int day, a;
	float money;
    day = 0;
  
    for(a = 2; a < 100 ; 	a *= 2)
    {
    
    
       money += a * 0.8;		//第一天2个,第2天4个,每天买的数目渐渐上升,钱就要边加变算。
       day++;
	}
	
	money = money / day ;
	printf("  %.06f  \n",  money );
 } 

Guess you like

Origin blog.csdn.net/qq_41070511/article/details/109989122