Why is celsius = 5 * (fahr - 32) / 9 ?
There is a program
to print Fahrenheit-Celsius table as below.
#include <stdio.h> /* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */
int main()
{
int fahr, celsius;
int lower, upper, step; lower = 0; /* lower limit of temperature table */
upper = 300; /* upper limit */
step = 20; /* step size */ fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}
The right part of the
figure is the output of this program. The Celsius temperature is computed and assigned to the variable celsius by
the statement
celsius = 5 * (fahr - 32) / 9;
The reason for multiplying by 5 and then dividing by 9 instead of just multiplying by 5/9 is that in C, as in many other languages, integer
division truncates: any fractional part is discarded. Since 5 and 9 are integers, 5/9 would be truncated to zero and so all the Celsius temperatures would be reported as zero.
Reference
Why is celsius = 5 * (fahr - 32) / 9 ?的更多相关文章
- Empire C:Basic 1
一.首先,从一个C程序开始: ///Name:Hello World ///Author:JA ///Date:2015-2-4 #include <stdio.h> //包含标准库的信息 ...
- C语言第一次博客作业—输入输出
一.PTA实验作业 题目1:7-3 温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1 ...
- C语言第一次作业——输入输出格式
题目1温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代码 #include& ...
- C语言第一次博客作业---顺序机构基础练习
一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...
- 第一周C语言作业
一.PTA实验作业 题目1.温度转换 1.实验代码 int main() { int fahr = 150,celsius; celsius = 5 * (fahr - 32) / 9; printf ...
- C语言第五次作业——循环结构
C语言程序设计第五次作业--循环结构(1) (一)改错题 输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增 ...
- C语言程序设计第五次作业——循环结构(1)
一.改错题 1.题目 输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增加2℉.计算公式如下:c = 5×( ...
- C语言程序设计第五次作业——循环结构
(一)改错题 1.题目:输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增加2℉.计算公式如下: c = 5 ...
- 第五次C语言作业
(一)改错题 输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增加2℉.计算公式如下: c = 5×(f-3 ...
随机推荐
- 关于"cin>>"输入成功或失败时的“返回值”(转载)
今天在看c++primer的时候,读到其中这样一段话: When we use an istream as a condition, the effect is to test the state o ...
- Servlet基础知识(四)——Servlet过滤器Filter
一.什么是过滤器: 政府大楼的安检保安,它既能对进入政府大楼的人员进行检查,只允许检查符合要求的进入:同时他也负责对出大楼的人进行检查,看他带出的东西是否符合要求. 同样的,Servlet中的过滤器既 ...
- python一些模块的exe安装包在windows的64位系统里识别不到已安装Python目录的解决方法
在windows里安装python一些模块时候,有时候源码安装比较困难,pip install也各种报错,这时候最喜欢用别人编译好的exe或者whl文件来安装,但是在windows的64位系统里,如果 ...
- java如何引入接口中定义的常量
接口 (A.java) : package config; public interface A { String PROJECT_ROOT_DIR = System.getProperty(&quo ...
- finally块的问题(finally block does not complete normally) (转)
当finall块中包含return语句时,Eclipse会给出警告“finally block does not complete normally”,原因分析如下: 1.不管try块.catch块中 ...
- CCNP路由实验(2) -- OSPF
OSPF作为一种内部网关协议(IGP),用于在同一个AS中的路由器之间交换路由信息.OSPF的特性如下:1.可适应大规模网络2.收敛速度快3.无路由环路4.支持VLSM和CIDR5.支持等价路由6.支 ...
- Java 初学者帮助文档以及基础教程
一下午的时间,大致看了一下Java的文档,进一步熟悉了Java的大体框架和结构,整理了一下有用的资源. 帮助文档: JSE 8 API 英文版 在线HTML格式:http://docs.oracle. ...
- Eclipse连接sql server 2012数据库编程一条龙
一.java通过jdbc连接sql server 2012 原帖地址:http://blog.csdn.net/stewen_001/article/details/19553173/ 1.sql s ...
- uva 10548 - Find the Right Changes(拓展欧几里得)
题目链接:uva 10548 - Find the Right Changes 题目大意:给定A,B,C,求x,y,使得xA+yB=C,求有多少种解. 解题思路:拓展欧几里得,保证x,y均大于等于0, ...
- 图画hadoop -- 生态圈