C Primer Plus第五章编程练习答案(完整版)

C Primer Plus 第五章编程练习答案(完整版)

本人是一名计算机专业在读学生,因为网上搜到的编程答案总是题号不完整,所以附上自己写的源代码,希望能够帮到大家,如有错误希望大家指正,也可以一起讨论,蟹蟹!
第一题

//5.11.1
#include<stdio.h>
#define convert 60;
int main()
{
	printf("Please tell us a time in minutes");
	int minutes,hours,mins;
	scanf("%d",&minutes);
	while (minutes > 0)
	{
		hours = minutes / convert;
		mins = minutes % convert;
		printf("%d minutes = %dhours ,%dmins\n",minutes,hours,mins);
		printf("Enter another number(Enter o to quit)");
		scanf("%d",minutes);

	}
	return 0;
}

第二题

//5.11.2
#include<stdio.h>
int main()
{
	printf("Please input a number");
	int a,b;
	scanf("%d",&a);
	b=a+10;
	while (a<=b)
	{
		printf("%d\n",a);
		a++;
	}
	return 0;
 } 

第三题

//5.11.3
#include<stdio.h>
int main()
{
	const int per = 7;
	printf("Please input a number of days:");
	int num,weeks,days;
	scanf("%d",&num);//输入天数
	while (num > 0)
	{
		weeks = num / per;
		days = num % per;
		printf("%d days are %d weeks, %d days.\n",num,weeks,days);
		printf("Enter the number of days(0 or less to quit!)");
		scanf("%d",&num);
		
	}
	return 0;
}

第四题

//5.11.4
#include<stdio.h>
int main()
{
	const float per1 = 2.54;//1inch=2.54cm
	const int per2 = 12; //1feet=12inches
	printf("Enter a height in centimeters: ");
	float h_c,h_i;
	int h_f;
	scanf("%f",&h_c);
	while (h_c > 0)
	{
		h_f = (int)h_c/per1/per2;
		h_i = h_c/per1 - per2*h_f;
		printf("%.1f cm = %d feet, %.1f inches\n",h_c,h_f,h_i);
		printf("Enter a height in centimeters (<=0 to quit):");
		scanf("%f",&h_c);
	}
	return 0;
}

第五题

#include<stdio.h>
/* 5.11.5 */
int main(void) 
{
	int count, sum,n;
	printf("Enter the upper limit: ");
	scanf("%d", &n);
	count = 0;
	sum = 0;
	while (count++ < n)
	{
	sum = sum + count;
	printf("sum = %d\n", sum);
	}
	return 0;
}

第六题

#include<stdio.h>
/* 5.11.6 */
int main(void) 
{
	int count, sum,n;
	printf("Enter the upper limit: ");
	scanf("%d", &n);
	count = 0;
	sum = 0;
	while (count++ < n)
	{
	sum = sum + count*count;
	printf("sum = %d\n", sum);
	}
	return 0;
}

第七题


/* 5.11.7 */
#include <stdio.h>
void Cube(double x);
int main() 
{
	double t;
	printf("Enter a floating-point value: ");
	scanf("%lf", &t);
	Cube(t);
	return 0;
}
void Cube(double x)
{
	printf("The cube of %f is %f.\n", x, x*x*x );
}

第八题

//5.11.8
#include<stdio.h>
int main()
{
	printf("This program computes moduli.\n");
	int a,b,c;
	printf("Enter an integer to serve as the second operand: ");
	scanf("%d",&b);
	printf("Now enter the first operand: ");
	scanf("%d",&a);
	while (a > 0)
	{
		c=a%b;
		printf("%d %% %d is %d\n",a,b,c);
		printf("Enter next number for first operand (<= 0 to quit):");
		scanf("%d",&a);
	}
	return 0;
}

第九题

//5.11.9
#include<stdio.h>
void Temperatures(double fah);
int main()
{
	double fah,cel,kel;
	printf("Please input the temperature in Fahrenheit:");
	while (scanf("%lf",&fah) == 1)
	{
		Temperatures(fah);
		printf("Please input another temperature(Enter q or other characters to quit)");
	}
	return 0;
}
void Temperatures(double fah)
{
	const double a = 5.0;
	const double b = 9.0;
	const double c = 32.0;
	const double d = 273.16;
	printf("%.2f ℉ is %.2f ℃\n",fah,a/b*(fah-c));
	printf("%.2f ℉ is %.2f K\n",fah,a/b*(fah-c)+d);
}

接下来会陆续做后面章节的答案,一起进步!

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值