C语言程序设计——现代方法(第2版)课后编程题答案

C语言程序设计——现代方法(第2版)课后编程题答案[作者:K.N.King 运行环境:visual studio 2019 c++]第二章至第六章

  • 第二章
    • 2.1 略
    • 2.2-2.3
    • 2.4
    • 2.5-2.6
    • 2.7
    • 2.8
  • 第三章
    • 3.1
    • 3.2
    • 3.3
    • 3.4
    • 3.5
    • 3.6
  • 第四章
    • 4.1
    • 4.2
    • 4.3
    • 4.4
    • 4.5
    • 4.6
  • 第五章
    • 5.1
    • 5.2
    • 5.3
    • 5.4
    • 5.5
    • 5.6
    • 5.7
    • 5.8
    • 5.9
    • 5.10
    • 5.11
  • 第六章
    • 6.1
    • *6.2
    • 6.3
    • 6.4
    • 6.5
    • 6.6
    • 6.7
    • 6.8
    • 6.9
    • 6.10
    • 6.11-6.12

第二章

2.1 略

2.2-2.3

题目:
请添加图片描述
解答:

#include 
#define pi 3.14

int main()
{
	int r = 0;
	printf("请输入半径:");
	scanf_s("%d", &r);
	printf("球体体积为:%f\n", 4.0f / 3.0f * pi * r * r * r);
	return 0;
}

2.4

请添加图片描述

#include 

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

%f:105;
%1f:105.0;
%2f:105.00;

2.5-2.6

C语言程序设计——现代方法(第2版)课后编程题答案_第1张图片

#include 

int main()
{
	int x, y;
	printf("请输入x:");
	scanf_s("%d", &x);
	y = ((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6;
	printf("%d", y);
	return 0;
}

2.7

C语言程序设计——现代方法(第2版)课后编程题答案_第2张图片

#include 

int main()
{
	int money;
	int num20,num10,num5,num1;
	printf("Enter a dollar amount:");
	scanf_s("%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

C语言程序设计——现代方法(第2版)课后编程题答案_第3张图片

#include 

int main()
{
	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;
}

第三章

3.1

请添加图片描述

#include 

int main()
{
	int year, month, day;
	printf("Enter a data(mm/dd/yyyy):");
	scanf_s("%d/%d/%d", &month, &day, &year);
	printf("You entered the date %d%.2d%.2d\n", year, month, day);
	//%.2d表示至少要显示两位数![请添加图片描述]
	return 0;
}

3.2

C语言程序设计——现代方法(第2版)课后编程题答案_第4张图片

#include 

int main()
{
	int number;
	float price;
	int year, month, day;
	printf("Enter item number:");
	scanf_s("%d", &number);
	printf("Enter unit price:");
	scanf_s("%f", &price);
	printf("Enter purchase date(mm/dd/yyyy):");
	scanf_s("%d/%d/%d", &month, &day, &year);
	printf("Item\tUnit\tPurchase\n    \tPrice\tDate\n%d\t%.2f\t%d%.2d%.2d\n",number,price, year, month, day);
	return 0;
}

3.3

C语言程序设计——现代方法(第2版)课后编程题答案_第5张图片

#include 

int main()
{
	int gp, gi, pc, in, cd;
	printf("Enter ISBN:");
	scanf_s("%d-%d-%d-%d-%d", &gp, &gi, &pc, &in, &cd);
	printf("Gs1 prefix: %d\n", gp);
	printf("Group identifier: %d\n", gi);
	printf("Publisher code: %d\n", pc);
	printf("Item number: %d\n", in);
	printf("Check digit: %d\n", cd);
	return 0;
}

3.4

请添加图片描述

#include 
 
int main (void)
{
	int a, b, c;
	printf ("Enter phone number [(xxx)xxx-xxxx]: ");
	scanf_s("(%d)%d-%d", &a, &b, &c);
	printf ("You entered %d.%d.%d", a, b, c);
	return 0;
	
}

3.5

C语言程序设计——现代方法(第2版)课后编程题答案_第6张图片

#include 

int main()
{
	int a[4][4];
	printf("Enter the numbers from 1 to 16 in any order:");
	for (int i = 0; i <= 3; i++)
	{
		for (int j = 0; j <= 3; j++)
		{
			scanf_s("%d", &a[i][j]);
		}
	}
//打印矩阵
	for (int i = 0; i <= 3; i++)
	{
		for (int j = 0; j <= 3; j++)
		{
			if (j != 3)
				printf("%d ", a[i][j]);
			else
				printf("%d\n", a[i][j]);
		}
	}
//计算row,col,diag
	int row, col, diag;
	row = col = diag = 0;
	printf("\nRow sums:");
	for (int i = 0; i <= 3; i++)
	{
		for (int j = 0; j <= 3; j++)
		{
			row = row + a[i][j];
			if (j == 3)
			{
				printf("%d  ", row);
				row = 0;
			}
		}
	}

	printf("\nColumn nums:");
	for (int i = 0; i <= 3; i++)
	{
		for (int j = 0; j <= 3; j++)
		{
			col = col + a[j][i];
			if (j == 3)
			{
				printf("%d  ", col);
				col = 0;
			}
		}
	}

	printf("\nDiagonal nums:");
	for (int i = 0; i <= 3; i++)
	{
		for (int j = 0; j <= 3; j++)
		{
			if (i == j)
			{
				diag = diag + a[i][j];
				if (j == 3)
				{
					printf("%d  ", diag);
					diag = 0;
				}
			}
		}
	}
	for (int i = 0; i <= 3; i++)
	{
		for (int j = 0; j <= 3; j++)
		{
			if (i + j == 3)
			{
				diag = diag + a[i][j];
				if (i == 3)
				{
					printf("%d  ", diag);
				}
			}
		}
	}
	return 0;
}

3.6

请添加图片描述
C语言程序设计——现代方法(第2版)课后编程题答案_第7张图片

#include 
 
int main (void)
{
	int num1, denom1, num2, denom2, result_num, result_denom;
	printf ("Enter two fractions separated by a plus sign: ");
	scanf_s("%d/%d+%d/%d", &num1, &denom1, &num2, &denom2);
	result_num = num1 *denom2 + num2 * denom1;
	result_denom = denom1 * denom2;
	printf ("The sum is %d/%d", result_num, result_denom);
	return 0;
}

第四章

4.1

C语言程序设计——现代方法(第2版)课后编程题答案_第8张图片

#include 

int main()
{
	int num,n,m;
	printf("Enter a two-digit number:");
	scanf_s("%d", &num);
	n = num / 10;
	m = num % 10;
	printf("The reversal is:%d%d", m, n);
	return 0;
}

4.2

#include 

int main()
{
	int num,n,m,p;
	printf("Enter a three-digit number:");
	scanf_s("%d", &num);
	n = num / 100;
	m = num %100;
	p = num % 10;
	m = (m - p) / 10;
	printf("The reversal is:%d%d%d", p,m, n);
	return 0;
}

4.3

请添加图片描述
C语言程序设计——现代方法(第2版)课后编程题答案_第9张图片

其中%1d表示一位整数(限定作用)

#include 

int main()
{
	int n,m,p;
	printf("Enter a three-digit number:");
	scanf_s("%1d%1d%1d", &n,&m,&p);
	printf("The reversal is:%d%d%d", p,m, n);
	return 0;
}

4.4

C语言程序设计——现代方法(第2版)课后编程题答案_第10张图片

#include 

int main()
{
	int num, a[5];
	printf("Enter a number between 0 and 32767:");
	scanf_s("%d", &num);
	for (int i = 0; i <= 4; i++)
	{
		a[4-i] = num % 8;
		num = num / 8;
	}
	printf("In octal,your number is :");
	for (int j = 0; j <= 4; j++)
	{
		printf("%d", a[j]);
	}
	return 0;
}

4.5

请添加图片描述

C语言程序设计——现代方法(第2版)课后编程题答案_第11张图片

#include 
 
int main (void)
{
	int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5;
	int first_sum, second_sum, total;
	
	printf ("Enter the frist 11 digits of a UPC: ");
	scanf_s("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",&d, &i1, &i2, &i3, &i4, &i5, &j1, &j2, &j3, &j4, &j5);
	first_sum = d + i2 + i4 + j1 + j3 + j5;
	second_sum = i1 + i3 + i5 + j2 + j4;
	total = 3 * first_sum + second_sum;
	
	printf ("Check digit: %d\n", 9 - ((total - 1) % 10));
	return 0;
}

4.6

C语言程序设计——现代方法(第2版)课后编程题答案_第12张图片

#include 
 
int main (void)
{
	int i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12;
	int first_sum, second_sum, total;
	
	printf ("Enter the frist 12 digits of a UPC: ");
	scanf_s("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8, &i9, &i10, &i11, &i12);
	first_sum = i2 + i4 + i6 + i8 + i10 + i12;
	second_sum = i1 + i3 + i5 + i7 + i9 + i11;
	total = 3 * first_sum + second_sum;
	
	printf ("Check digit: %d\n", 9 - ((total - 1) % 10));
	return 0;
}

第五章

5.1

C语言程序设计——现代方法(第2版)课后编程题答案_第13张图片

#include 

int main()
{
	int num, a;
	printf("Enter a number: ");
	scanf_s("%d", &num);

	if (num < 10)
		a = 1;
	else if (num < 100)
		a = 2;
	else if (num < 1000)
		a = 3;
	else if (num < 10000) 
		a = 4;
	else{
		printf("没有输入五位数以内的数字或输入了负数,无法判断");
	}
	printf("The number %d has %d digits.", num, a);
	return 0;
}

5.2

请添加图片描述

#include 

int main()
{
	int hour, a, min;

	printf("Enter a 24-hour time: ");
	scanf_s("%d:%d", &hour, &min);

	printf("Equivalent 12-hour time: ");
	if (hour > 12) {
		a = hour - 12;
		printf("%d:%.2d", a, min);
	}
	else {
		printf("%d:%.2d", hour, min);
	}

	if(hour <= 12)
	{
		printf("  AM");
	}
	else printf("  PM");
	return 0;
}

5.3

请添加图片描述

5.4

C语言程序设计——现代方法(第2版)课后编程题答案_第14张图片

5.5

C语言程序设计——现代方法(第2版)课后编程题答案_第15张图片

int main()
{
	float income, tax;

	printf("Enter the income value: ");
	scanf_s("%f", &income);

	if (income < 750.0f) 
		tax = income * 0.01;
	else if (income < 2250.0f) 
		tax = 7.5f + (income - 750.0f) * 0.02;
	else if (income < 3750.0f) 
		tax = 37.5f + (income - 2250.0f) * 0.03;
	else if (income < 5250.0f) 
		tax = 82.5f + (income - 3750.0f) * 0.04;
	else if (income < 7000.0f)
		tax = 142.5f + (income - 5250.0f) * 0.05;
	else tax = 230.0f + (income - 7000.0f) * 0.06;

	printf("The tax to be paid is %.4f", tax);
	return 0;
}

5.6

请添加图片描述
C语言程序设计——现代方法(第2版)课后编程题答案_第16张图片

#include 

int main()
{
	int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5;
	int first_sum, second_sum, total,check,digit;

	printf("Enter the frist 11 digits of a UPC: ");
	scanf_s("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &d, &i1, &i2, &i3, &i4, &i5, &j1, &j2, &j3, &j4, &j5);
	first_sum = d + i2 + i4 + j1 + j3 + j5;
	second_sum = i1 + i3 + i5 + j2 + j4;
	total = 3 * first_sum + second_sum;

	printf("Enter the digit:");
	scanf_s("%d", &digit);
	check = 9 - ((total - 1) % 10);
	if (digit == check)
		printf("VALID!");
	else printf("NOT VALID!");
	return 0;
}

5.7

C语言程序设计——现代方法(第2版)课后编程题答案_第17张图片

#include 

int main()
{
	int a[4],temp;
	printf("Enter four integers:");
	for (int i = 0; i <= 3; i++)
	{
		scanf_s("%d", &a[i]);
	}
	//冒泡排序
	for (int j = 0; j <= 2; j++)
	{
		for (int i = 0; i <= 2-j; i++)
		{
			if (a[i] > a[i + 1])
			{
				temp = a[i + 1];
				a[i + 1] = a[i];
				a[i] = temp;
			}
		}
	}
	printf("Largest:%d\n", a[3]);
	printf("Smallest:%d\n", a[0]);

	return 0;
}

5.8

C语言程序设计——现代方法(第2版)课后编程题答案_第18张图片

#include 

int main()
{
	int hour, minute;
	int time;

	printf("Enter a 24-hour time:");
	scanf("%d:%d", &hour, &minute);

	time = hour * 60 + minute;
	if (time < 480) 
		printf("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
	else if (time < 583){
		if ((time - 480) < (583 - time)) printf("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
		else printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
	}
	else if (time < 679) {
		if ((time - 583) < (679 - time)) printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
		else printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m");
	}
	else if (time < 767) {
		if ((time - 679) < (767 - time)) printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
		else printf("Closest departure time is 12:47 a.m., arriving at 3:00 p.m");
	}
	else if (time < 840) {
		if ((time - 767) < (840 - time)) printf("Closest departure time is 12:47 a.m., arriving at 3:00 p.m.");
		else printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
	}
	else if (time < 945) {
		if ((time - 840) < (945 - time)) printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
		else printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
	}
	else if (time < 1140) {
		if ((time - 945) < (1140 - time)) printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
		else printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
	}
	else {
		if ((time - 1140) < (1305 - time)) printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
		else printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
	}

	return 0;
}

5.9

请添加图片描述

#include 

int main()
{
	int year1, month1, day1;
	int year2, month2, day2;
	printf("Enter first date(mm/dd/yyyy):");
	scanf_s("%d/%d/%4d", &month1, &day1, &year1);
	printf("Enter second date(mm/dd/yyyy):");
	scanf_s("%d/%d/%4d", &month2, &day2, &year2);
	if (year2 > year1)
	{
		printf("%d/%d/%d is ealier than %d/%d/%d", month1, day1, year1, month2, day2, year2);
	}
	else if (year2 == year1)
	{
		if(month2>month1) printf("%d/%d/%d is ealier than %d/%d/%d", month1, day1, year1, month2, day2, year2);
		else if (month2 == month1)
		{
			if (day2 > day1) printf("%d/%d/%d is ealier than %d/%d/%d", month1, day1, year1, month2, day2, year2);
			else if (day2 == day1) printf("They are same days");
			else printf("%d/%d/%d is ealier than %d/%d/%d", month2, day2, year2, month1, day1, year1);
		}
		else printf("%d/%d/%d is ealier than %d/%d/%d", month2, day2, year2, month1, day1, year1);
	}
	else printf("%d/%d/%d is ealier than %d/%d/%d", month2, day2, year2, month1, day1, year1);
	return 0;
}

5.10

C语言程序设计——现代方法(第2版)课后编程题答案_第19张图片

#include 
 
int main ()
{
	int num;
	
	printf ("Enter numerical grade: ");
	scanf_s("%d", &num);
	
	if (num < 0 || num >100) {
		printf ("Illegal input!");
		return 0;
	}
	
	switch (num/10) {
		case 10:
		case 9 :
			printf ("Lstter grade: A");
			break;
		case 8 :
			printf ("Lstter grade: B");
			break;
		case 7 :
			printf ("Lstter grade: C");
			break;
		case 6 :
			printf ("Lstter grade: D");
			break;
		default :
			printf ("Lstter grade: F");
			break;
	}
	return 0;	
} 

5.11

C语言程序设计——现代方法(第2版)课后编程题答案_第20张图片

#include 
 
int main ()
{
	int num;
	printf ("Enter a two-digit number: ");
	scanf_s("%d", &num);
	
	if (num < 10 || num > 99) {
		printf ("Illegal input!");
		return 0;
	}
	
	printf("You entered the number ");
	switch (num / 10) {
		case 9:printf ("ninety");break;
		case 8: printf ("eighty");break;
		case 7:printf ("seventy");break;
		case 6:printf ("sixty");break;
		case 5:printf ("fifty");break;
		case 4:printf ("fourty");break;
		case 3:printf ("thirty");break;
		case 2:printf ("twenty");break;
		case 1:
			switch (num % 10) {
				case 0: printf ("ten"); break;
				case 1: printf ("eleven"); break;
				case 2: printf ("twelve"); break;
				case 3: printf ("thirteen"); break;
				case 4: printf ("fourteen"); break;
				case 5: printf ("fifteen"); break;
				case 6: printf ("sixteen"); break;
				case 7: printf ("seventeen");break;
				case 8: printf ("eighteen"); break;
				case 9: printf ("nineteen"); break;
			}
			return 0;
	}
	
	switch (num % 10) {
		case 9:printf ("-nine");break;
		case 8: printf ("-eight");break;
		case 7:printf ("-seven");break;
		case 6:printf ("-six");break;
		case 5:printf ("-five");break;
		case 4:printf ("-four");break;
		case 3:printf ("-three");break;
		case 2:printf ("-two");break;
		case 1:printf ("-one");break;
		case 0:break;
	}
	return 0;
}

第六章

6.1

C语言程序设计——现代方法(第2版)课后编程题答案_第21张图片

#include 

int main()
{
	float a,max;
	printf("Enter a number:");
	scanf_s("%f", &a);
	max = a;
	while (a > 0)
	{
		printf("Enter a number:");
		scanf_s("%f", &a);
		if (a > max) max = a;
	}
	printf("The largest number entered is %f", max);
	return 0;
}

最后的输出格式有点问题,如果想限定两位小数的输出可以改为%.2f

*6.2

C语言程序设计——现代方法(第2版)课后编程题答案_第22张图片

#include 

int main()
{
	int m,n,p;
	printf("Enter two integers:");
	scanf_s("%d,%d", &m,&n);
	while (n != 0)
	{
		p = n;
		n = m % n;
		m = p;
	}
	printf("Greatest common divisor is %d", m);
	return 0;
}

6.3

C语言程序设计——现代方法(第2版)课后编程题答案_第23张图片

#include 

int main()
{
	int m,n,p,a,b;
	printf("Enter a fraction:");
	scanf_s("%d/%d", &a,&b);
	m = a; n = b;
	while (n != 0)
	{
		p = n;
		n = m % n;
		m = p;
	}
	if (m)
	{ 
		a = a / m;
		b = b / m;
	}
	printf("In lowest terms:%d/%d", a,b);
	return 0;
}

6.4

C语言程序设计——现代方法(第2版)课后编程题答案_第24张图片

#include 
 
int main ()
{
	float value, commission = 0.0f;
	
	printf ("Enter value of trade: ");
	scanf_s ("%f", &value);
	
	while (value != 0.0f) {
		if (value < 2500.0f){
			commission = 30.0f + 0.017f * value;
		} else if (value < 6250.0f) {
			commission = 56.0f + 0.0066f * value;
		} else if (value < 20000.0f) {
			commission = 76.0f + 0.0034f * value;
		} else if (value < 50000.0f) {
			commission = 100.0f + 0.0022f * value;
		} else if (value < 500000.0f) {
			commission = 155.0f + 0.0011f * value;
		} else {
			commission = 255.0f + 0.0009f * value;
		}
		if (commission < 39) commission = 39;
		
		printf ("Commission: $%.2f\n\n", commission);
		
		printf ("Enter value of trade: ");
		scanf_s ("%f", &value);
	}
 
	
	return 0;	
} 

6.5

请添加图片描述

#include 

int main()
{
	int num, i,a[10];//可以处理十位数及以内
	i = 0;
	printf("Enter a  number:");
	scanf_s("%d", &num);
	do
	{	
			a[i] = num % 10;
			num = num / 10;
			i++;
	}
	while (num != 0);
	printf("The reversal is:");
	for (int j = 0;j<i;j++)
	{
		printf("%d", a[j]);
	}
	return 0;
}

6.6

C语言程序设计——现代方法(第2版)课后编程题答案_第25张图片

#include 

int main()
{
	int num, n;
	printf("Enter a number:");
	scanf_s("%d", &num);
	for (int i = 0; i <= 100; i++)
	{
		n = (2*i) * (2*i);
		if (num >= n) printf("%d\n", n);
	}
	return 0;
}

6.7

请添加图片描述
C语言程序设计——现代方法(第2版)课后编程题答案_第26张图片

#include 
 
int main ()
{
	int n, odd, square;
	
	printf ("This program prints a table of squares.\n");
	printf ("Enter number of entries in table: ");
	scanf_s ("%d", &n);
	
	odd = 3;
	for (int i = 1, square = 1; i <= n; odd += 2, ++i) {
		printf ("%10d%10d\n", i, square);
		square += odd;
	}	
	return 0;
}

6.8

C语言程序设计——现代方法(第2版)课后编程题答案_第27张图片

#include 
 
int main ()
{
	int day, week;
	
	printf ("Enter number of days in month: ");
	scanf_s ("%d", &day);
	printf ("Enter starting day of the week (1=Sun, 7=Sat): ");
	scanf_s ("%d", &week);
	
	for (int i = week; i > 1; i--) printf ("   ");
	for (int i = 1; i <= day; i++, week++) {
		printf ("%3d", i);
		if (week == 7) {
			week = 0;
			printf ("\n");	
		}
	}
	return 0;
}

6.9

请添加图片描述

#include 
 
int main ()
{
	float money, rate, payment; 
	
	printf ("Enter amount of loan:");
	scanf_s ("%f", &money);
	printf ("Enter interest rate:");
	scanf_s ("%f", &rate);
	printf ("Enter monthly payment:");
	scanf_s ("%f", &payment);
	
	int num;
	printf ("Enter the number of repayments:(1~10) ");
	scanf_s ("%d", &num); 
	 
	for (int i = 1; i <= num; i++){
		printf ("Balance remaining after ");
		switch (i) {
			case 1: printf ("1st"); break;
			case 2: printf ("2nd"); break;
			case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:printf ("%dth", i);break;
		}
		printf (" payment:%.2f\n", money = ((money - payment) + (money * rate * 0.01/ 12)));
	}
	return 0;
}

6.10

C语言程序设计——现代方法(第2版)课后编程题答案_第28张图片

#include 

int main()
{
	int year1, month1, day1;//令1为最早的日期,2为新输入的日期
	int year2, month2, day2;

	printf("Enter a date(mm/dd/yyyy):");
	scanf_s("%d/%d/%4d", &month1, &day1, &year1);
	printf("Enter a date(mm/dd/yyyy):");
	scanf_s("%d/%d/%4d", &month2, &day2, &year2);

	while (month2 != 0 && day2 != 0 && year2 != 0)
	{
		if (year2 > year1) {}//1就是最小的,不执行语句
		else if (year2 == year1)
		{
			if (month2 > month1) {}
			else if (month2 == month1)
			{
				if (day2 > day1) {}
				else if (day2 == day1) printf("They are same days");
				else {
					month1 = month2;
					day1 = day2;
					year1 = year2;
				}

			}
			else {
				month1 = month2;
				day1 = day2;
				year1 = year2;
			}
		}
		else {
			month1 = month2;
			day1 = day2;
			year1 = year2;
		}
		printf("Enter a date(mm/dd/yyyy):");
		scanf_s("%d/%d/%4d", &month2, &day2, &year2);
	}

	printf("%d/%d/%d is the earliest date.", month1, day1, year1);
	return 0;
}

6.11-6.12

C语言程序设计——现代方法(第2版)课后编程题答案_第29张图片

#include 
//6.11
int main()
{
	int num;
	float e, m;
	e = 0;m = 1;
	printf("Enter a number:");
	scanf_s("%d", &num);
	for (int i = 1; i <= num; i++)
	{
		int j = i;
		while (j >0)
		{
			m *= j;
			j--;
		}
		e += 1.0f/m;
	}
	printf("The value of e is %f", e);
	return 0;
}
#include 
//6.12
int main()
{
	float e, m,num;
	e = 0; m = 1;
	printf("Enter a number:");
	scanf_s("%f", &num);
	int i = 1;
	while (e <= num)
	{
		int j = i;
		while (j > 0)
		{
			m *= j;
			j--;
		}
		e += 1.0f / m;
		i++;
	}
	printf("The value of e is %f", e);
	return 0;
}

你可能感兴趣的:(c语言练习题,c语言,c++,开发语言)