C Primer Plus 5.11 编程练习

1.

#include <stdio.h>
#define MIN 60

int main(void)
{
	const int h = 60;
	
	int m, hours, minute;
	
	printf("Please enter a minute (<= 0 to quit):\n");
	
	scanf("%d", &m);
	
    while(m > 0)
	{
		hours = m / h ;
		
		minute = m % MIN;
		
		printf("The %d minutes is %d hours and %d minutes.\n", m, hours, minute);
	    
		printf("Please enter a minute (<= 0 to quit):\n");
		
		scanf("%d", &m);
			
	}
	
	printf("That's all.\n");
	
	return 0;
}

2.

方法①:

#include <stdio.h>

int main(void)
{
	int q, s;
	 
	
	printf("Please enter a number:");
	
	scanf("%d", &q);
	
	s = q + 11;
	
	while (q < s)
	{
		printf("%d \t", q);
		
		q++; 
	}
	
	printf("\nThat's all.\n");	
	
	return 0;
}

方法②:

# include <stdio.h>

int main(void)
{
	int a, b;
	
	a  =  -1;
	
	printf("Please enter a number:");
	
	scanf("%d", &b);
	
	while (a++ < 10)
	      printf("%d \n", b+a);
	
	return 0;
}

3

#include <stdio.h>
#define WEEK 7

int main(void)
{
	int c, d, e;
		
	printf("Please enter a number (<= 0 to quit):");
	
	scanf("%d", &e);
	
	while(e > 0)
	{
		c = e / WEEK;
		
		d = e % WEEK;
		
		printf("%d days are %d weeks, %d days.\n", e, c, d);
		
		printf("Please enter a number(<=0 to quit):");
		
		scanf("%d", &e);
		
	}
		
	printf("END !");
	
	return 0;
}

4.

#include <stdio.h>
#define CMTOINCH 0.3937008
#define CMTOFEET 0.032808
#define FEETTOINCH 12

int main(void)
{
    float height, inch;
	
	int feet;
	
	printf("Enter a height in centimeters(<= 0 to quit):");
	
	scanf("%f", &height);
	
    while (height > 0)
    {
    	feet = height * CMTOFEET;  //feet是整数声明,故小数部分会被截断保留整数部分赋值给feet。 
		
		inch = (height * CMTOINCH) - (FEETTOINCH * feet);
    
    	printf("%.1f cm = %d feet, %.1f inches\n", height, feet, inch);
    	
		printf("Enter a height in centimeters(<= 0 to quit):");
    	
		scanf("%f", &height); 	
    	
	}
	
	printf("bye");
	
	return 0;
}

5.

#include <stdio.h>

int main(void)
{
	int count = 0, sum = 0;
	
	int days;
	
	printf("Please enter the number of days:\n");
	
	scanf("%d", &days);
	
	while(count++ < days)
	     
		  sum = sum + count;
	
	printf("In past %d days we can earn $%d money.\n", days, sum);		
	
	return 0;
}

6.

#include <stdio.h>

int main(void)
{
	int count = 0, sum = 0;
	
	int days;
	
	printf("Please enter the number of days:\n");
	
	scanf("%d", &days);
	
	while(count++ < days)
	     
		  sum = sum + count * count;
	
	printf("In past %d days we can earn $%d money.\n", days, sum);		
	
	return 0;
}

7.

#include <stdio.h>

void cube(double a);

int main (void)
{
	double num;
	
	printf("Enter a number:");
	
	scanf("%lf", &num);

/*
scanf() 函数对于 double 类型变量,格式化字符串必须使用 %lf。
如果使用 %f ,则会导致未定义行为(通常会导致读取错误,
因为 %f 期望的是 float 类型的指针,而传入的是 double 类型的指针)。
printf() 函数则可以用 %f 或 %lf 来表示 double 类型变量
*/
	
	cube (num);
	
	return 0;

}

void cube (double a)
{
	double  c;
	
	c = a * a * a;

    printf("The number's cube is %.2f\n", c);	
	
	
}

8.

#include <stdio.h>

int main(void)
{
	int f, s, t;
	
	printf("This program computes moduli.\n");
	
	printf("Enter an integer to serve as the second operand: ");
	
	scanf("%d", &s);
	
	printf("Now enter the first operand: ");
	
	scanf("%d", &f);
	
	printf("%d %% %d is %d\n", f, s, f % s);
	
	printf("Enter next number for first operand (<= 0 to quit): ");
	
	scanf("%d", &t);
	
	while(t > 0)
	{
		printf("%d %% %d is %d\n", t, s, t % s);
	    
		printf("Enter next number for first operand (<= 0 to quit): ");
	    
		scanf("%d", &t);
		
	}
	
	printf("Done");
	
	return 0;
}

9.

#include <stdio.h>

void Temperatures(double h);

int main(void)
{
	double hua, she, kai, t;
	
	printf("Please enter a temperature(not a number or q to quit) :");
 
	while(scanf("%lf", &hua) == 1)
	{		
		  Temperatures(hua);
	
	   	  printf("Please enter a temperature(not a number or q to quit) :");

    }
	
	printf("Done!");
	
	return 0;
}

void Temperatures(double h)
{
	double she, kai;
	
	const float a = 5.0, b = 9.0, c = 32.0, d = 273.16;
	
	she = a / b * (h - c);
	
	kai = she + d;
	
	printf("\nFahrenheit:%.2lf centigrade:%.2lf kelvin:%.2lf\n\n", h, she, kai);
	
	
 } 
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值