欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C语言程序设计现代方法第二版,第二章课后编程习题全部答案

程序员文章站 2024-02-29 11:17:28
...

自己练习时手写,难免会有些疏忽遗漏等各种各样问题,错误之处还请指出

但这些代码确实已通过编译,实现了书上的输出结果,还希望能给抱有期待之人作为个小参考

后续章节慢慢更新中


2.1

#include <stdio.h>

int main (void)
{
	printf ("       *\n");
	printf ("      *\n");
	printf ("     *\n");
	printf ("*   *\n");
	printf (" * *\n");
	printf ("  *\n");
	
	return 0;
} 

2.2&2.3

#include <stdio.h>

#define pi 3.14

int main (void) 
{
	int r = 0;
	
	scanf ("%d", &r);
	
	printf ("%f", 4.0f/3.0f*pi*r*r*r);
	
	return 0;
}

2.4

#include <stdio.h>

int main (void)
{
	float a;
	
	printf ("Enter an amount:");
	scanf ("%f", &a);
	
	printf ("With tax added: $%.2f", a*1.05);
	
	
	return 0;
}

2.5&2.6

#include <stdio.h>

int main (void)
{
	int x, y;
	
	scanf ("%d", &x);
	
	y = ((((3*x+2)*x-5)*x-1)*x+7)*x-6;
	
	printf ("%d", y);
	
	return 0;
}

2.7

#include <stdio.h>

int main (void)
{
	int money;
	int num20, num10, num5, num1;
	
	printf ("Enter a dollar amount:");
	scanf ("%d", &money);
	
	num20 = money / 20;
	money = money - 20*num20;
	num10 = money / 10;
	money = money - 10*num10;
	num5 = money / 5;
	num1 = money % 5;
	
	printf ("$20 bills: %d\n", num20);
	printf ("$10 bills: %d\n", num10);
	printf (" $5 bills: %d\n", num5);
	printf (" $1 bills: %d\n", num1);
	
	return 0;
}

2.8

#include <stdio.h>

int main (void)
{
	float money, rate, payment; 
	
	printf ("Enter amount of loan:");
	scanf ("%f", &money);
	printf ("Enter interest rate:");
	scanf ("%f", &rate);
	printf ("Enter monthly payment:");
	scanf ("%f", &payment);
	
	printf ("Balance remaining after first payment:%.2f\n", money=((money-payment)+(money*0.01*rate/12)));
	printf ("Balance remaining after second payment:%.2f\n", money=((money-payment)+(money*0.01*rate/12)));
	printf ("Balance remaining after third payment:%.2f\n", money=((money-payment)+(money*0.01*rate/12)));
	
	return 0;
}