文章目录
1、假如我国国民生产总值的年增长率为10%,计算10年后我国国民生产总值与现在相比增长多少百分比?
思路:计算公式为:
r为年增长率,n为年数,p为与现在相比的倍数。可以pow函数求y的x次方的值,调用pow函数的具体形式是pow(x,y)。在使用pow函数时需要在程序开头用#include指令将<math.h>头文件包含到本程序模块中。具体实现代码如下图所示:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float p, r, n;
r = 0.1;
n = 10;
p = pow(1 + r, n);
printf("p=%f\n", p);
system("pause");
return 0;
}
输出结果:
2、求整数位数
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a;
scanf("%d",&a);
int sum =0; //sum是一个计数器
while(a>0)
{
sum++;
a/=10;
}
switch(sum)
{
case 1:
printf("个 \n");
break;
case 2:
printf("十 \n");
break;
case 3:
printf("百 \n");
break;
}
system("pause");
return 0;
}
3、在屏幕上输出一个等腰三角形
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a,b;
int num = 0; // 代表行的数量
scanf ("%d",&num); //输入行数
for(a=1; a<num; a++)
{
for(b = 0; b<num-a;b++)
{
printf(" ");
}
for(b =0; b < a*2 -1; b++)
{
printf("*");
}
printf ("\n");
}
system("pause");
return 0;
}
输出结果:
4、有3个整数a,b,c,由键盘输入,输出其中最大的数
程序的思路如下:
程序如下所示:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c;
printf("请输入三个整数:");
scanf_s("%d,%d,%d", &a, &b, &c);
if (a < b)
if (b < c)
printf("max=%d\n", c);
else
printf("max=%d\n", b);
else if (a<c)
printf("max=%d\n", c);
else
printf("max=%d\n", a);
system("pause");
}
程序执行结果如下: