A program to print Fahrenheit-Celsius table with floating-point values
Another
program to print Fahrenheit-Celsius table with decimal integer
This program is presented
as below.
#include <stdio.h>
/* print Fahrenheit_Celsius table
for fahr = 0, 20, ..., 300; floating-point version */
int main()
{
float fahr, celsius;
int lower, upper, step; lower = 0; /* lower limit of temperature table */
upper = 300; /* upper limit of temperature table */
step = 20; /* step size */ fahr = lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
} return 0;
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJuZXJ3YW5nMjAxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
The figure of this program is presented as above. The right part of the figure is the output. This is much like the program which is mentioned at the beginning of the article, except that fahr and celsius are
declared to be float. We were unable to use 5/9 in the previous version because integer division would truncate it to zero. A
decimal point in a constant indicates that it is floating point, however, so 5.0/9.0 is not truncated because it is the ratio of two floating-point values.
If an arithmetic operator has integer operands, an integer operation is performed. If
an arithmetic operator has one floating-point operand and one integer operand, however, the integer will be converted to floating point before the operation is done. Writing floating-point constants with explicit decimal points even when
they have integral values emphasizes their floating-point nature for human readers.
For now, notice that the assignment
fahr = lower;
and the test
while (fahr <= upper)
also work in the nature way — the int is converted to float before the operation is done.
The implications of width and precision are tabled as follows.
- %d print as decimal integer
- %6d print as decimal integer, at least 6 characters wide
- %f print as foating point
- %6f print as floating point, at least 6 characters wide
- %.2f print as floating point, 2 characters after decimal point
- %6.2f print as floating point, at leat 6 wide and 2 characters after decimal point
Among others, printf also recognizes %o for
octal, %x for hexadecimal, %c for
character, %s for charater string, and %% for % itself.
Reference
A program to print Fahrenheit-Celsius table with floating-point values的更多相关文章
- @media print样式 关于table断页
<html> <head> <style> @media print { table { page-break-after:auto } tr { page-bre ...
- python3变量和数据类型
变量和数据类型 知识点 python 关键字 变量的定义与赋值 input() 函数 字符串的格式化 实验步骤 每一种编程语言都有它们自己的语法规则,就像我们所说的外语. 1. 关键字和标识符 ...
- Python3简明教程(二)—— 变量和数据类型
关键字和标识符 下列的标识符是Python3的关键字,并且不能用于通常的标识符.关键字必须严格按照下面的拼写: False def if raise None del import return Tr ...
- ABAP program lines are wider than the internal table
错误详细描述: An exception occurred that is explained in detail below.The exception, which is assigned to ...
- Why is celsius = 5 * (fahr - 32) / 9 ?
Go to my personal blog There is a program to print Fahrenheit-Celsius table as below. #include <s ...
- Codeforces Round #344 (Div. 2) B. Print Check
B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Python multi-thread 多线程 print 如何避免print的结果混乱
multithread如何写 这是我第一次写multithread,所以就是照着例子学,下面是我用来学的例子 来自于”Automate the boring stuff with Python”的15 ...
- Calling Lua From a C Program
Introduction From a running C program, you can call a Lua script. The C program can pass arguments t ...
- Codeforces Round #344 (Div. 2) B. Print Check 水题
B. Print Check 题目连接: http://www.codeforces.com/contest/631/problem/B Description Kris works in a lar ...
随机推荐
- JAVA基础——设计模式之简单工厂模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述简单工厂模式的:简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式.简单工厂模式是由一个工厂 ...
- 解决mysql时区问题以及SSL问题
看了下网上的教程,觉得都太麻烦啦,这里推荐个简单的! 解决时区问题 只需要加上serverTimezone=UTC即可,如下: spring.datasource.url=jdbc:mysql://1 ...
- java 常用集合类型--以及其特性
1:集合: (1) Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,查询快,增 ...
- IP_MULTICAST_LOOP
WINDOWS 中 该选项仅控制接收部分.即设置为0 则控制套接字无法接收自身消息.设置为1 则控制套接字使能接收自身消息. LINUX 中 该先项仅控制发送部分.即设置为0 则控制套 ...
- 洛谷 3871 [TJOI2010]中位数
[题解] 平衡树模板题,不过因为可以离线,所以有别的做法.把询问倒着做,变成删掉数字.求中位数,于是可以二分+树状数组. #include<cstdio> #include<cstr ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- git clone问题
中秋节回来上班 竟然忘记带电脑了  ̄□ ̄||还好同事有备用电脑,这要是回去拿估计上午都不用干什么了,用同事电脑当然需要安装环境,下面说一下git上遇到的问题吧 (1)首先我尝试用https方式克隆代码 ...
- 贪心算法求解活动安排<算法分析>
一.实验内容及要求 1.要求按贪心算法原理求解问题: 2.要求手工输入s[10]及f[10],其中注意自己判断s[i]<f[i]: 3.要求显示所有活动及最优活动安排的i事件列表.二.实验步骤 ...
- 怎样在WINDOWS下面编译OpenSSL
编译OPENSSL的步骤: 第一步:下载ActivePerl(http://www.activestate.com/, ),安装ActivePerl,默认安装路径在C:\Perl64.打开命令提示符, ...
- hihoCoder#1119 小Hi小Ho的惊天大作战:扫雷·二
原题地址 没有复杂算法,就是麻烦,写起来细节比较多,比较考验细心,一次AC好开心. 代码: #include <iostream> #include <vector> #inc ...