C语言现代方法:第六章课后编程操作练习(6,7,9-12题解答思路)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()//第六题do语句写法
{
	int b, c, d;
	scanf("%d", &b);

	c = 0, d = 0;

	do{
		c = c + 2;
		d = c * c;
		if (d >b)
			break;
		printf("%d\n", d);
	} while (d <= b);

	return 0;
}
int main()//第六题while 语句写法
{
	int b, c, d;
	scanf("%d", &b);

	c = 0, d = 0;

	while (d < b){
		c = c + 2;
		d = c * c;
		printf("%d\n", d);
	} 

	return 0;
}


int main()//第七题
{
	int i, n, odd, square;

	printf("This program prints a table of square.\n");
	printf("Enter number of entries in table: ");
	scanf("%d", &n);
	square = 1;
	odd = 3;
	for (i = 1;i<=n;i+=1) {
		printf("%10d%10d\n", i, square);
		square += odd;
		odd += 2;
	}
	return 0;
}
int main()//第九题
{
	float loan, rate, payment, a, b, c;
	int n;

	printf("Enter amount of loan: ");
	scanf("%f", &loan);
	printf("Enter interest rate: ");
	scanf("%f", &rate);
	printf("Enter monthly payment: ");
	scanf("%f", &payment);
	printf("Enter the number of payment: ");
	scanf("%f", & a);

	b = loan * (rate / 100 / 12 + 1) - payment;

	for (n = 1;n <= a;n+=1) {
		c = b;
		printf("Balance remaining after %d payment; %0.2f\n", n, b);
		b = c * (rate / 100 / 12 + 1) - payment;
	}

	return 0;
}


int main()//第十题
{
	int dd, mm, yy, earl_d, earl_m, earl_y, earliest_d, earliest_m, earliest_y;

	printf("Enter a date (mm/dd/yy): ");
	scanf("%d/%d/%d", &dd, &mm, &yy);

	earliest_d = dd;
	earliest_m = mm;
	earliest_y = yy;

	do {
		printf("Enter a date (mm/dd/yy): ");
		scanf("%d/%d/%d", &earl_m, &earl_d, &earl_y);

		if (earl_d == 0 && earl_m == 0 && earl_y == 0)
			break;

		if (earliest_y > earl_y)
			earliest_m = earl_m, earliest_d = earl_d, earliest_y = earl_y;
		else 
			earliest_m = mm, earliest_d = earl_d, earliest_y = earl_y;

		if(earliest_m >earl_m && earliest_y ==earl_y)
			earliest_m = earl_m, earliest_d = earl_d, earliest_y = earl_y;
		if (earliest_m < earl_m && earliest_y == earl_y)
			earliest_m = mm, earliest_d = earl_d, earliest_y = earl_y;

		if (earliest_d > earl_d && earliest_m == earl_m && earliest_y == earl_y)
			earliest_m = earl_m, earliest_d = earl_d, earliest_y = earl_y;
		if (earliest_d < earl_d && earliest_m == earl_m && earliest_y == earl_y)
			earliest_m = mm, earliest_d = earl_d, earliest_y = earl_y;
		

	} while (1);

	printf("%d/%d/%d is the earliest date", earliest_m, earliest_d, earliest_y);
	return 0;
}

int main()//第十一题
{
	int i, n;
	float sum = 1, a = 1;

	printf("Enter a number: ");
	scanf("%d", &n);

	for (i = 1;i <= n;i++) {
		a = a * i;
		sum = sum + 1/a;
	}
	printf("sum: %.4f", sum);
	return 0;
}

int main()//第十二题
{
	int i=1;
	float sum = 1, a = 1,x;

	printf("Enter a number: ");
	scanf("%f", &x);

	for (;;) {
		a = a * i;
		sum = sum + 1/a;
		i++;
		if (sum >= x)
			break;
	}
	printf("n: %d", i);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值