C Primer Plus 第5章 运算符、表达式和语句 编程练习

时间:2024-07-30 13:34:26

1、

#include <stdio.h>
const int S_PER_M = ; int main(void)
{
int min, hour, lmin; printf("请输入分钟数: \n");
scanf("%d", &min);
while(min > )
{
hour = min / S_PER_M;
lmin = min % S_PER_M;
printf("%d分钟 = %d小时, %d分钟\n", min, hour, lmin);
scanf("%d", &min);
}
printf("Done!\n");
return ;
}

2、

#include <stdio.h>

int main(void)
{
int num, i = -; printf("请输入一个数: \n");
scanf("%d", &num);
printf("输出为: \n");
while(i++ < )
printf("%d ", num + i);
return ;
}

3、

#include <stdio.h>

int main(void)
{
int days, week, ldays; printf("请输入天数: \n");
scanf("%d", &days);
while(days > )
{
week = days / ;
ldays = days % ;
printf("%d days are %d week, %d days.\n", days, week, ldays);
printf("请输入天数: \n");
scanf("%d", &days);
}
printf("Done!\n");
return ;
}

4、

#include <stdio.h>
#define CAST1 0.3937008
#define CAST2 12
int main(void)
{
float cm, inch;
int feet; printf("Enter a height in centimeters: ");
scanf("%f", &cm);
while(cm > )
{
feet = (int)(cm * CAST1) / CAST2;
inch = cm * CAST1 - feet * CAST2;
printf("%.1f cm = %d feet, %.1f inches\n", cm, feet, inch);
printf("Enter a height in centimeters (<= 0 to quit): ");
scanf("%f", &cm);
}
printf("bye\n");
return ;
}

5、

#include <stdio.h>

int main(void)
{
int count, sum, number;
count = ;
sum = ;
printf("请您输入一个数: \n");
scanf("%d", &number);
while(count++ < number)
sum += count;
printf("1 + 2 + 3 + ... + %d = %d\n", number, sum);
return ;
}

6、

#include <stdio.h>

int main(void)
{
int count, sum, number;
count = ;
sum = ;
printf("请您输入一个数: \n");
scanf("%d", &number);
while(count++ < number)
sum += count * count;
printf("1~%d的平方之和为: %d\n", number, sum);
return ;
}

7、

#include <stdio.h>
void print(float fl); int main(void)
{
float n;
printf("请输入一个float类型的数: \n");
scanf("%f", &n);
print(n);
return ;
} void print(float fl)
{
printf("%.2f的立方为: %.2f\n", fl, fl * fl * fl);
}

8、

#include <stdio.h>
void Temperatures(double db); int main(void)
{
double fahrenheit;
printf("请输入一个华氏温度: \n");
while(scanf("%lf", &fahrenheit))
{
Temperatures(fahrenheit);
printf("请输入一个华氏温度: \n");
}
printf("bye\n");
return ;
}
void Temperatures(double db)
{
const double cast1 = 1.8;
const double cast2 = 32.0;
const double cast3 = 273.16;
double celsius, kelvin;
celsius = cast1 * db + cast2;
kelvin = celsius + cast3;
printf("华氏温度%.2f\n------>摄氏温度为%.2f\n------>绝对温度为%.2f\n", db, celsius, kelvin);
}