A program-to-print Fahrenheit-celsius table with floating-point values

Source: Internet
Author: User
Tags integer division

Go to my personal blog


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, ..., floating-point version */int mai N () {    float Fahr, celsius;    int lower, upper, step;    lower = 0;      /* Lower limit of temperature table */    upper =;     /* Upper limit of temperature table */    step =;       /* 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;}




The figure of this program is presented as above. The right part of the and is the output. This was much like the program which was mentioned at the beginning of the article, except thatfahrandcelsiusis 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's floating point, however, so 5.0/9.0 are not truncated because it is the ratio of the floating-point values.

If an arithmetic operator have integer operands, an integer operation is performed. If An arithmetic operator have one floating-point operand and one integer operand, however, the integer would be con Verted to floating point before the operation are done.Writing floating-point constants with explicit decimal points even when they has integral values emphasizes their floatin G-point Nature for Human readers.

For now, notice the assignment

Fahr = lower;  


And the test

while (Fahr <= Upper)

Also work in the Nature way-the int are converted to float before the operation are 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 in leat 6 wide and 2 characters after decimal point

Among others,printfalso recognizes%oFor octal,%xFor hexadecimal,%cFor character,%sFor Charater string, and%%For%itself.

Referencethe C progrmming Language

A program-to-print Fahrenheit-celsius table with floating-point values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.