双语版c++ 第三章答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Chapter Three Keyboard Input and Screen Output
Exercises
1.Write a program to input four numbers and display them in reverse order.
#include<stdio.h>
int main(){
printf("Please input four numbers:");
int a,b,c,d;
scanf("%d%d%d%d",&d,&c,&b,&a);
printf("%d%d%d%d\n",a,b,c,d);
return0;
}
2.Write a program that inputs a number of hours and displays the equivalent number of weeks,days and hours.For example,an input of553should display3weeks,2days and1hour.
#include<stdio.h>
int main(){
int hours;
int week,day,hourLeft;
printf("Please input the hours:");
scanf("%d",&hours);
week=hours/24/7;
day=hours/24%7;
hourLeft=hours-hours/24*24;
printf("%d weeks,%d days,%d hours\n",week,day,hourLeft);
return0;
}
3.Assuming the human heart rate is seventy-five beats per minute;write a program to ask a user their age in years and to calculate the number of beats their heart has made so far in their life.Ignore leap years.(leap years:闰年)
#include<stdio.h>
int main(){
int years;
printf("Please input your age:");
scanf("%d",&years);
printf("the number of beats your heart has made so far in your life is%d\n",years*75*60*24*365);
return0;
}
4.Write a program to accept a temperature in degrees Fahrenheit and convert it to degrees Celsius. Your program should display the following prompt:
Enter a temperature in degrees Fahrenheit:
You will then enter a decimal number followed by the Enter key.
The program will then convert the temperature by using the formula
Celsius=(Fahrenheit-32.0)*(5.0/9.0)
Your program should then display the temperature in degrees Celsius using an appropriate message.
#include<stdio.h>
int main(){
double FahrenheitT;
printf("Enter a temperature in degrees Fahrenheit:");
scanf("%lf",&FahrenheitT);
double CelsiusT=(FahrenheitT-32.0)*(5.0/9.0);
printf("the temperature in degrees Celsius is:%lf\n",CelsiusT);
return0;
}
5.Make changes to the program in exercise4to accept the temperature in degrees Celsius and convert it to degrees Fahrenheit.(Fahrenheit:华氏温度;Celsius:摄氏温度)
#include<stdio.h>
int main(){
double CelsiusT;
printf("Enter a temperature in degrees Celsius:");
scanf("%lf",&CelsiusT);
double FahrenheitT=9.0*CelsiusT/5.0+32;
printf("the temperature in degrees Fahrenheit is:%lf\n",FahrenheitT);
return0;
}
6.Write a program to accept a distance in kilometres and display the equivalent distance in miles.
(1mile=1.609344kilometres.)
#include<stdio.h>
int main(){
double kilometres;
printf("Enter a distance in kilometres:");
scanf("%lf",&kilometres);
double miles=kilometres/1.609344;
printf("the equivalent distance in miles:%lf\n",miles);
return0;
}
7.Write a program to input three floating-point numbers from the keyboard and to calculate
(a)their sum and
(b)their average
Display the results to three decimal places.
#include<stdio.h>
int main(){
float a,b,c;
float sum,avg;
printf("Please input three floating-point numbers:\n");
scanf("%f%f%f",&a,&b,&c);
sum=a+b+c;
avg=sum/3;
printf("their sum is%.3f and their average is:%.3f\n",sum,avg);
return0;
}
8.Write a program to read in two numbers from the keyboard and to display the result of dividing the second number into the first.
For example,if the input is123and12,the result should be displayed in the following format:
123divided by12=10Remainder=3
(Hint:use the modulus operator%to get the remainder3,and use integer division to get the quotient 123.)(modulus operator:求余运算符)
#include<stdio.h>
int main(){
int a,b;
printf("Please input two int number:\n");
scanf("%d%d",&a,&b);
printf("%d divided by%d=%d Remainder=%d\n",a,b,a/b,a%b);
return0;
}。

相关文档
最新文档