一
// 编写一个程序,把分钟表示的时间转换成小时和分钟表示的时间。使用
// #define或const创建一个表示60的符号常量或const变量。通过while循环
// 让用户重复输入值,直到用户输入小于或等于0的值才停止循环。
#include"stdio.h"
#define HOUR 60
int main(void)
{
int minutes;
printf("Please enter how many minutes.\n");
scanf("%d",&minutes);
while(minutes>0)
{
printf("%dhours and %dminutes\n",minutes/HOUR,minutes%HOUR);
printf("Please enter how many minutes again.");
scanf("%d",&minutes);
}
return 0;
}
二
// 编写一个程序,提示用户输入一个整数,然后打印从该数到比该数大
// 10的所有整数(例如,用户输入5,则打印5~10的所有整数,包括5和15)
// 要求打印的各值之间用一个空格、制表符或换行符分开。
#include"stdio.h"
int main(void)
{
int n,m;
printf("Please enter an integer.\n");
scanf("%d",&n);
m=n;
printf("%d\n",n);
while(n++<m+10)
printf("%d\n",n);
return 0;
}
三
// 编写一个程序,提示用户输入天数,然后将其转换成周数和天数。
// 例如,用户输入18,则转换成2周4天。以下面的格式显示结果:
// 18 days are 2 weeks,4 day
// 通过while循环让用户重复输入天数,当用户输入一个非正值时(如0或-20)
//循环结束。
#include"stdio.h"
#define week 7
int main(void)
{
int day,weeks;
printf("Please enter the number of days.");
scanf("%d",&day);
while(day>0)
{
printf("%d days are %d weeks,%d days\n",day,day/week,day%week);
printf("Please enter the number of days again.");
scanf("%d",&day);
}
return 0;
}
四
// 编写一个程序。提示用户输入一个身高(单位:厘米),并分别以厘米和英寸为单位
// 显示该值,允许有小数部分。程序应该能然用户重复输入身高,直到用户输入一个非正值。
// 其输入如下:
// Enter a height in centimeters:182
// 182.0cm=5feet,11.7inches
// Enter a height in centimeters(<=0 to quit):168.7
// 168.7cm=5feet,6.4 inches
// Enter a height in centimeters(<=0 to quit):0
// bye
#include"stdio.h"
#define in 12
#define fe 2.54
int main(void)
{
int feet;
float height,inch,inc;
printf("Enter a height in centimeters:");
scanf("%f",&height);
while(height>0)
{ inch=height/fe;
feet=inch/in;
inc=inch-feet*in;
printf("%f %d %f\n",inch,feet,inc);
printf("%.1f cm= %d feet,%.1f inches\n",height,feet,inc);
printf("Enter a height in centimeters (<=0 to quit):");
scanf("%f",&height);
}
return 0;
}
五
// 修改程序addemup.c(程序清单5.13),你可以认为addemup.c是计算
// 20天里赚多少钱的程序(假设第一天赚$1、第二天赚$2、第三天赚$3,以此类推)
// 修改程序,使其可以与用户交互,根据用户输入的数进行计算(即,用读入的
// 一个变量来代替20)
#include <stdio.h>
int main(void)
{
int count,sum;
count=0;
sum=0;
int end;
scanf("%d",&end);
while(count++<end)
sum=sum+count;
printf("sum=%d\n",sum);
return 0;
}
六
// 修改编程练习5的程序,使其能计算整数的平方和(可以认为第一天赚$1、
// 第二天赚$4、第三天赚$9,以此类推,这看起来还不错)。c没有平方函数,
// 但是可以用n*n来表示n的平方。
#include <stdio.h>
int main(void)
{
int count,sum;
count=0;
sum=0;
int end;
scanf("%d",&end);
while(count++<end){
sum+=count*count;
}
printf("sum=%d\n",sum);
return 0;
}
七
// 编写一个程序。提示用户输入一个double类型的数,并打印该数的立方值。
// 自己设计一个函数计算并打印立方值。main()函数要把用户输入的值传递
// 给该函数。
#include <stdio.h>
void pouch(double n);
int main(void)
{
double m;
printf("please enter a double number\n");
scanf("%lf",&m);
pouch(m);
return 0;
}
void pouch(double n)
{
printf("%.2lf",n*n*n);
}
八
// 编写一个程序,显示求模运算的结果。把用户输入的第一个整数作为求模
// 运算符的第二个运算对象,该数在运算过程中保持不变。用户后面输入的数
// 是1个运算对象。当用户输入一个非正值时,程序结束。其输出实例如下:
// This program computes moduli.
// Enter an integer to sarve as the first 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 m,n;
printf("This program computes moduli.\n");
printf("Enter an integer to sarve as the first second operand:");
scanf("%d",&m);
printf("Now enter the first operand:");
scanf("%d",&n);
printf("%d%%%d=%d\n",n,m,n%m);
printf("Enter next number for first operand (<=0 to quit):");
scanf("%d",&n);
while(n>0){
printf("%d%%%d=%d\n",n,m,n%m);
printf("Enter next number for first operand (<=0 to quit):");
scanf("%d",&n);
}
printf("Done");
return 0;
}
九
// 编写一个程序,要求用户输入一个华氏温度。程序应该读取double类型的值
// 作为温度值,并把该值作为参数传递给一个用户自定义的函数Temperatures()。
// 该函数计算摄氏温度和开氏温度,并以小数点后面两位数字的精度显示3中温度。
// 要使用不同的温标来表示这3个温度值。下面是华氏温度转摄氏温度的公式:
// 摄氏温度=2.0/9.0*(华氏温度-32.0)
// 开氏温标常用于科学研究,0表示绝对零。下面是摄氏温度转开氏温度的公式:
// 开氏温度=摄氏温度+273.16
// Temperatures()函数中用const创建温度转换中使用的变量。再main()函数中
// 使用一个循环让用户重复输入温度,当用户输入q或其他非数字时,循环结束。scanf()
// 函数返回读取数据的数量,所以如果读取数字则返回1,如果读取q则不返回1.可以使用
// ==运算符将scanf()的返回值和1作比较,测试两值是否相等。
#include <stdio.h>
void temperatures(double m);
int main(void)
{
double wendu;
int b;
printf("Please enter a fahrenheit temperatures: ");
b=scanf("%lf",&wendu);
while(b==1){
temperatures(wendu);
b=scanf("%lf",&wendu);
}
return 0;
}
void temperatures(double m)
{
double n;
const double sheshi=5.0/9.0;
const double kaishi=273.16;
n=sheshi*(m-32.0);
printf("Fahrenheit is %.2lf\n",m);
printf("Degree centigrade is %.2lf\n",n);
printf("Degree kelvin is %.2lf\n",n+kaishi);
}
如有问题请留言,会及时回复。