1.编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间。使用#define或const创建一个表示60的符号常量或const变量。通过while 循环让用户重复输入值,直到用户输入小于或等于0的值才停止循环。
#include <stdio.h>
int main() {
const int A = 60;
int min, hour, sec;
printf("请输入分钟数:");
scanf_s("%d", &min);
while (min > 0) {
hour = min / A;
sec = min % A;
printf("%d 分钟是 %d 个小时 %d 分钟.按0退出.\n", min, hour, sec);
scanf_s("%d", &min);
}
return 0;
}
2.编写一个程序,提示用户输入一个整数,然后打印从该数到比该数大 10的所有整数(例如,用户输入5,则打印5~15的所有整数,包括5和 15)。要求打印的各值之间用一个空格、制表符或换行符分开。
#include <stdio.h>
int main() {
int num,i;
printf("请输入一个整数:");
scanf_s("%d", &num);
for (i = 0; i <= 10; i++) {
printf("%d\n", num);
num += 1;
};
return 0;
}
#include <stdio.h>
int main(){
int the_num, num;
printf("请输入数字:");
scanf("%d",&num);
num = num - 1;
the_num = num + 10;
while(num++ <= the_num)
printf("%d\n",num);
return 0;
}
3.编写一个程序,提示用户输入天数,然后将其转换成周数和天数。例 如,用户输入18,则转换成2周4天。以下面的格式显示结果:
18 days are 2 weeks, 4 days.
通过while循环让用户重复输入天数,当用户输入一个非正值时(如0 或-20),循环结束。
#include <stdio.h>
#define DAY 7
int main() {
int week, day, days;
printf("please enter a integer:");
scanf_s("%d", &days);
while (days > 0) {
week = days / DAY;
day = days % DAY;
printf("%d days are %d weeks, %d days.\n", days, week, day);
printf("please enter a integer:");
scanf_s("%d", &days);
};
return 0;
}
4.编写一个程序,提示用户输入一个身高(单位:厘米),并分别以厘米和英寸为单位显示该值,允许有小数部分。程序应该能让用户重复输入身高,直到用户输入一个非正值。其输出示例如下:
Enter a height in centimeters: 182
182.0 cm = 5 feet, 11.7 inches
Enter a height in centimeters (<=0 to quit): 168.7
168.0 cm = 5 feet, 6.4 inches
Enter a height in centimeters (<=0 to quit): 0
bye
#include <stdio.h>
int main() {
float height, inches;
int feet;
printf("请输入你的身高(cm):");
scanf_s("%f", &height);
while (height > 0) {
inches = height * 0.39;
feet = height / 30.48;
printf("%.1f cm = %d feet, %.1f inches ", height, feet, inches);
printf("Enter a height in centimeters (<=0 to quit):");
scanf_s("%f", &height);
if (height <= 0)
printf("bye");
}
return 0;
}
5.5.修改程序addemup.c(程序清单5.13),你可以认为addemup.c是计算20天里赚多少钱的程序(假设第1天赚$1、第2天赚$2、第3天赚$3,以此类 推)。修改程序,使其可以与用户交互,根据用户输入的数进行计算(即, 用读入的一个变量来代替20)。
#include <stdio.h>
int main(void)
{
int count, sum;
sum = 0;
count = 0;
printf("请输入天数:");
scanf("%d", &count);
while(count>0)
sum = sum + count--;
printf("sum = %d\n", sum);
return 0;
}
6.修改编程练习5的程序,使其能计算整数的平方和(可以认为第1天赚 $1、第2天赚$4、第3天赚$9,以此类推,这看起来很不错)。C没有平方函 数,但是可以用n * n来表示n的平方。
#include <stdio.h>
int main(void)
{
int count, sum;
sum = 0;
count = 0;
printf("请输入天数:");
scanf("%d", &count);
while(count>0){
sum = sum + count*count;
count--;
}
printf("sum = %d\n", sum);
return 0;
}
7.编写一个程序,提示用户输入一个double类型的数,并打印该数的立方值。自己设计一个函数计算并打印立方值。main()函数要把用户输入的值 传递给该函数。
#include <stdio.h>
int compute(double a){
double e;
e = a*a*a;
return e;
}
int main(viod){
double num1;
printf("请输入一个double类型的数字:\n");
scanf("%lf",&num1);
compute(num1);
printf("经计算得:%f",num1);
return 0;
}
8.编写一个程序,显示求模运算的结果。把用户输入的第1个整数作为求模运算符的第2个运算对象,该数在运算过程中保持不变。用户后面输入的数是第1个运算对象。当用户输入一个非正值时,程序结束。其输出示例如下:
This program computes moduli.
Enter an integer to serve as the second operand: 256
Now enter the first operand: 438
438 % 256 is 182
Enter next number for first operand (<= 0 to quit): 1234567
1234567 % 256 is 135
Enter next number for first operand (<= 0 to quit): 0 Done
#include <stdio.h>
int main(void){
int num1,num2;
printf("This program computes moduli.\nEnter an integer to serve as the second operand: ");
scanf("%d",&num1);
printf("Now enter the first operand:");
scanf("%d",&num2);
while(num2>0) {
printf("%d %% %d = %d ", num2, num1, num2 % num1);
printf("Enter next number for first operand (<= 0 to quit):");
scanf("%d",&num2);
}
printf("Done\n");
return 0;
}
9.编写一个程序,要求用户输入一个华氏温度。程序应读取double类型 的值作为温度值,并把该值作为参数传递给一个用户自定义的函数Temperatures()。该函数计算摄氏温度和开氏温度,并以小数点后面两位数字 的精度显示3种温度。要使用不同的温标来表示这3个温度值。下面是华氏温 度转摄氏温度的公式:
摄氏温度 = 5.0 / 9.0 * (华氏温度 - 32.0)
开氏温标常用于科学研究,0表示绝对零,代表最低的温度。下面是摄氏温度转开氏温度的公式: 开氏温度 = 摄氏温度 + 273.16
Temperatures()函数中用const创建温度转换中使用的变量。在main()函数 中使用一个循环让用户重复输入温度,当用户输入 q 或其他非数字时,循环 结束。scanf()函数返回读取数据的数量,所以如果读取数字则返回1,如果读取q则不返回1。可以使用==运算符将scanf()的返回值和1作比较,测试两值是否相等。
#include <stdio.h>
int Temperture(double temp){
const double fun1 = 5.0 / 9.0;
const double fun12 = 32.0;
const double fun2 = 273.16;
double sheshi,kaishi;
sheshi = fun1 * (temp - fun12);
kaishi = sheshi + fun2;
printf("华式温度为:%.2lf,摄氏温度为:%.2lf,开式温度为:%.2lf\n",temp,sheshi,kaishi); //懒得去查英文名了,直接拼音了。。
return 0;
}
int main(){
double temp1;
printf("请输入华氏温度:");
while(scanf("%lf",&temp1)==1){
Temperture(temp1);
printf("输入q或其他字符退出\n");
}
printf("成功退出!\n");
return 0;
}