Exercises 2-12 of Zhejiang University Edition "C Language Programming (3rd Edition)"

Exercise 2-12 Output Fahrenheit-Celsius temperature conversion table (15 points)

Enter 2 positive integers lower and upper (lower≤upper≤100), please output a Fahrenheit-Celsius temperature conversion table with a value range of [lower, upper] and an increase of 2 degrees Fahrenheit each time.
The calculation formula for temperature conversion: C=5×(F−32)/9, where: C represents temperature in Celsius and F represents temperature in Fahrenheit.

Input format:
Enter 2 integers in one line, representing the values ​​of lower and upper, separated by spaces.
Output format: the
first line of output: "fahr celsius" and
then each line outputs a Fahrenheit temperature fahr (integer) and a Celsius temperature celsius (occupies 6 characters width, right-aligned, reserved 1 decimal).
If the input range is invalid, "Invalid." is output.
Input example 1:
32 35
Output example 1:
fahr celsius
32 0.0
34 1.1
Input example 2:
40 30
Output example 2:
Invalid.
Author
C Course Group
Unit
Zhejiang University

Code Length Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB

#include <stdio.h>
#include <math.h>

int main() {
    
    
    int lower, upper;
    if (scanf("%d %d", &lower, &upper) == 2) {
    
    
        if (lower <= upper && upper <= 100) {
    
    
            printf("fahr celsius\n");
            for (; lower <= upper; lower += 2) {
    
    
                printf("%d%6.1f\n", lower, 5 * (lower - 32) / 9.0);
            }
        } else {
    
    
            printf("Invalid.");
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/DoMoreSpeakLess/article/details/109249531