C Primer Plus编程练习答案(Chapter5)

5.11.1

#include<stdio.h>
#define N 60 
int main() {
	float min, hour, sec;
	
	printf("Please enter minute(<=0 quit):");
	scanf_s("%f", &min);

	while (min > 0) {
		hour = min / N;
		sec = min * N;
		printf("There are %.2f hours.\n", hour);
		printf("There are %.0f seconds.\n", sec);
		printf("\n");
		printf("Please enter minute(<=0 quit):");
		scanf_s("%f", &min);
	}

	printf("That's over.");
	return 0;
}

 5.11.2

#include <stdio.h>
int main() {
	int num, tempnum;

	printf("Please enter a integer:");
	scanf_s("%d", &num);
	tempnum = num + 10;

	while (num <= tempnum) {
		printf("%d\n", num);	// \n or \t or ' '
		num++;
	}
}

 5.11.3

#include <stdio.h>
int main() {
	int day, week, rest_day;

	printf("Please enter day number:");
	scanf_s("%d", &day);

	while (day > 0) {
		week = day / 7;
		rest_day = day % 7;
		printf("%d days are %d weeks, %d days.\n", day, week, rest_day);

		printf("\nPlease enter day number:");
		scanf_s("%d", &day);
	}
	printf("Wrong day number, over.");

	return 0;
}

 5.11.4

#include <stdio.h>
#include <stdio.h>
#define N 2.54  //一英寸的厘米数
#define M 12    //一英尺的英寸数
int main() {
	float height, feet, inch;

	printf("Enter a height in centimeters: ");
	scanf_s("%f", &height);

	while (height > 0) {
		inch = height / N;
		feet = (int)(inch / M);
		inch = inch - M*feet;
		printf("%.1f cm = %.0f feet, %.1f inches",height, feet, inch);
		printf("\nEnter a height in centimeters (<=0 to quit):");
		scanf_s("%f", &height);
	}

	printf("bye");

	return 0;
}

 5.11.5

#include <stdio.h>
int main() {
	int day, money = 0;
	int i = 1;

	printf("Please enter the day number: ");
	scanf_s("%d", &day);

	while (i <= day) {
		money += i;
		i++;
	}
	printf("The money you earn is: $%d", money);

	return 0;
}

 5.11.6

#include <stdio.h>
int main() {
	int day, money = 0;
	int i = 1;

	printf("Please enter the day number: ");
	scanf_s("%d", &day);

	while (i <= day) {
		money += i * i;
		i++;
	}
	printf("The money you earn is: $%d", money);

	return 0;
}

 5.11.7

#include <stdio.h>
void cube(double NUM) {
	NUM = NUM * NUM * NUM;
	printf("The cube of number is: %.2f", NUM);
}
int main() {
	double num;
	
	printf("Please enter a float number:");
	scanf_s("%lf", &num);
	cube(num);

	return 0;
}

 5.11.8

#include <stdio.h>
int main() {
	int fstnum, secnum, trdnum;

	printf("This program computes moduli.\n");
	printf("Enter an integer to serve as the second operand: ");
	scanf_s("%d", &secnum);
	printf("Now enter the first operand: ");
	scanf_s("%d", &fstnum);

	while (fstnum > 0) {
		trdnum = fstnum % secnum;
		printf("%d %% %d is %d\n", fstnum, secnum, trdnum);
		printf("Enter next number for first operand (<= 0 to quit): ");
		scanf_s("%d", &fstnum);
	}

	printf("Done.");

	return 0;
}

 5.11.9

 

#include <stdio.h>
void Temperatures(double Fdegree) {
	double Cdegree, Kdegree;
	Cdegree = 5.0 / 9.0 * (Fdegree - 32.0);
	Kdegree = Cdegree + 273.16;

	printf("The centiagred temperatures is: %.2f^C\n", Cdegree);
	printf("The Kelvin temperatures is : %.2fK\n", Kdegree);
}
int main() {
	double fdegree;
	int judge;

	printf("Please enter Fahrenheit degree: ");
	judge = scanf_s("%lf", &fdegree);
	while (judge == 1) {
		Temperatures(fdegree);
		printf("Please enter next Fahrenheit degree: ");
		judge = scanf_s("%lf", &fdegree);
	}

	printf("Done.");

	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值