C language | Landing freely, how many meters have passed for the 10th landing

Example 57: A ball falls freely from a height of 100m, rebounds back to half of its original height each time it hits the ground, falls again, and rebounds again. C language programming asks how many meters it passes when it hits the ground for the 10th time, and how high the rebound is for the 10th time.

Analysis: It should be easy for readers to understand when looking at the questions. To do this kind of questions, you must first observe the rules. This type of questions has rules. First observe the rules, and then think about how to write code. Readers need to pay attention to defining floating-point types when defining variables, not integer types.

Source code demo:

#include<stdio.h>//头文件 
int main()//主函数 
{
    
    
  double height,bounce_Height;//定义双精度浮点型变量 
  height=100;//赋初值 
  bounce_Height=height/2;//赋初值 
  int n;//定义整型变量 
  for(n=2;n<=10;n++)
  {
    
    
    height=height+2*bounce_Height;//第n次落地时共经过的米数 
    bounce_Height=bounce_Height/2;//第n次反跳高度 
  } 
  printf("第10次落地时共经过%f米\n",height);
  printf("第10次反弹%f米\n",bounce_Height);
  return 0; 
}

The compilation and running results are as follows:

10次落地时共经过299.609375米
第10次反弹0.097656--------------------------------
Process exited after 0.106 seconds with return value 0
请按任意键继续. . .

C language | Landing freely, how many meters have passed for the 10th landing

More cases can go to the public account: C language entry to mastery

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112438244