1.2 Variables and Arithmetic Expressions

 
  
/*Print Fahrenheit-Celsius table: for fahr =0, 20, ... ,300*/
void ConvertFahrenheittoCelsius()
{
    int Fahr, Celsius;
    int lower, higher, step;
    
    lower = 0;
    higher = 300;
    step = 20;

    while (lower < higher)
    {
        Fahr = lower;
        Celsius = 5 * (Fahr - 32) / 9;
        printf("\t%d\t%d\n", Fahr, Celsius);
        lower = lower + step;
    }
 
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            float Fahr, Celsius;
            int lower, higher, step;

            lower = 0;
            higher = 300;
            step = 20;

            Fahr = lower;
            while (Fahr < higher)
            {
                Celsius = (float)((5.0 / 9.0)*(Fahr - 32.0)); 
Console.WriteLine(
"{0}, \t {1}", Fahr, Celsius); Fahr = Fahr + step; } Console.ReadLine(); } } } 

#include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { float Fahr, Celsius; int lower, higher, step; lower = 0; higher = 300; step = 20;    Fahr = lower; /* The int is converted to float before the operation is done.*/ while (Fahr <= higher) { Celsius = (5.0 / 9.0) * (Fahr - 32); printf("%3.0f %6.1f \n", Fahr, Celsius); Fahr = Fahr + step; } return 0; }

 

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.

Printf is a general-purpose output formatting function, and it is just a useful function from the standard library of functions that are normally accessible to C programs.

  

In C Sharp,double cannot implicitly be converted to float.


The printf conversion specification %3.0f says that a floating-point number is to be printed at least three
characters wide, with no decimal point and no fraction digits.

转载于:https://www.cnblogs.com/limeina/p/3573708.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值