1.2 Variables and Arithmetic Expressions

作者在 2009-02-13 08:24:04 发布以下内容
The program uses the formula [ Celsius = (5/9) * (Fahrenheit - 32)] to print the following table of Fahrenheit temperatures and their centigrade or Celsius equivalents:
=============
/* print Fahrenheit - Celsius table for fahr =0, 20, ..., 300 */
#include <stdio.h>
 
int main(void)
{
    int fahr, cels;
    int low, upper, step;
 
    low   = 0;
    upper = 300;
    step  = 20;
 
    fahr = low;
    while (fahr <= upper)
    {
        cels = (5.0/9.0) * (fahr - 32);
        printf("%d\t%d\n", fahr, cels);
        fahr += step;
    }
 
    getch();
    return 0;
}
======================
/* print Fahrenheit - Celsius table for fahr = 0, 20, ..., 300;floating-point version */
#include <stdio.h>
int main(void)
{
    float fahr, cels;
    int low, upper, step;
   
    low   = 0;
    upper = 300;
    step  = 20;
   
    fahr = low;
    while ( fahr <= upper)
    {
          cels = (5.0/9.0) * ( fahr - 32);
          printf("%3.0f   %.1f\n",fahr, cels);
          fahr = fahr + step;
    }
   
    getch();
    return 0;
}
 
文章评论,共0条
游客请输入验证码
浏览27700次
文章归档