Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình C

Viết bảng chuyển đổi nhiệt độ bằng hàm trong C

Chương trình in bảng Fahrenheit → Celsius (0 đến 300, bước 20)

#include <stdio.h>

float conversion(float fh);

int main() {
    float fh, cl;
    int begin = 0, stop = 300;

    printf("Fahrenheit\tCelsius\n");
    printf("----------\t-----------\n");

    fh = begin;
    while (fh <= stop) {
        cl = conversion(fh);
        printf("%3.0f\t\t%6.1f\n", fh, cl);
        fh += 20;
    }
    return 0;
}

float conversion(float fh) {
    return (fh - 32) * 5 / 9;
}

Kết quả

Fahrenheit  Celsius
----------  -----------
0           -18
20          -7
40           4
60          16
80          27
100         38
120         49
140         60
160         71
180         82
200         93
220        104
240        116
260        127
280        138
300        149

Để chuyển Celsius sang Fahrenheit, thay công thức: fahrenheit = (celsius * 9 / 5) + 32.