第三章、数据和C 编程练习

版权声明:本文为博主原创文章,未经博主允许不得转载! https://blog.csdn.net/weixin_42839965/article/details/85010688

2、提示输入一个ASII码值,打印输入的字符。

#include <stdio.h>
int main(void)
{
	int n;
	printf("请输入数字:");
	scanf("%d",&n);
	printf("%d is %c\n", n, n);
	return 0;
}

3、发出警报,并打印

**Startled by the sudden sound,Sally shouted
"By the Great Pumpkin,what was that!"**
#include <stdio.h>

int main(void)
{
	printf("\a");
	printf("Startled by the sudden sound,Sally shouted\n");
	printf("\"By the Great Pumpkin,what was that!\"\n");

	return 0;
}

4、读取一个浮点数,先打印成小数点的形式,再打印成指数形式,若系统支持,打印成p计数法(即十六进制计数法)。

#include <stdio.h>

int main(void)
{
	float n;

	printf("Enter a floating-point value:");
	scanf("%f", &n);
	printf("fixed-point notation:%f\n",n);
	printf("exponential notation:%e\n",n);
	printf("p notation:%a\n",n);


	return 0;
}

5、一年大约有3.156X10^7秒,提示用户输入年龄,然后显示该年龄对应的秒数。

#include <stdio.h>

int main(void)
{
	int age;
	float t = 3.156e7;
	double seconds;

	printf("请输入用户年龄:");
	scanf("%d", &age);
	seconds = age*t;
	printf("该年龄对应的秒数:%.2f,\n用科学计数法即:%e\n", seconds, seconds);

	return 0;
}

调试图示
6、1个水分子的质量约为3.0X10^-23克,1夸克脱水大约是950克,提示用户输入水的夸脱数,并显示水分子的数量。

#include <stdio.h>

int main(void)
{
	int n;//水的夸脱数
	float m;//水分子的数量
	float t = 3.0e-23;//一个水分子的质量3.0X10ⁿ(其中n=-23)

	printf("请输入水的夸脱数:");
	scanf("%d", &n);
	m = n * 950 / t;
	printf("您输入的水的夸脱数是:%d,\n水分子的数量是:%e\n", n, m);

	return 0;
}

在这里插入图片描述
7、1英寸相当于2.54厘米,提示用户输入身高(/英寸),然后以厘米为单位显示单位身高。

#include <stdio.h>

int main(void)
{
	float high1, high2;
	printf("请输入身高(英寸):");
	scanf("%f", &high1);
	high2 = high1*2.54;
	printf("您输入的身高(英寸):%.2f,\n显示的身高(厘米)是:%.2f\n", high1, high2);

	return 0;
}

8、在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于3茶勺,编写程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。

#include <stdio.h>

int main(void)
{
	int n;
	float a, b, c, d;//a-品脱,b-盎司,c-汤勺,d-茶勺

	printf("请输入杯数:");
	scanf("%d", &n);
	
	a = (float)n / 2;//类型强行转换,否则是整型
	b = n * 8;
	c = b * 2;
	d = c * 3;

	printf("您输入的杯数是:%d,\n品脱数是:%.2f,\n盎司是:%.2f,\n汤勺是:%.2f,\n茶勺是:%.2f\n", n, a, b, c, d);

	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42839965/article/details/85010688
今日推荐