Experiment 2-3-5 Output Fahrenheit-Celsius temperature conversion table (15 points)

Enter 2 positive integers lower和upper(lower≤upper≤100), please output a [lower,upper]Fahrenheit-Celsius temperature conversion table with a value range of 2 degrees Fahrenheit each time.

The calculation formula of temperature conversion:, C=5×(F−32)/9where: C represents temperature in Celsius, F represents temperature in Fahrenheit.

Input format:

Enter 2 integers in a row, each representing lower和upperthe value, separated by a space.

Output format:

The first line of output:"fahr celsius"

Then output one Fahrenheit temperature fahr(integer) and one Celsius temperature celsius(occupies 6 characters width, right-aligned, and 1 decimal place) for each line.

If the input range is illegal, " 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.

Code:

# include <stdio.h>
# include <stdlib.h>

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

Submit screenshot:

Insert picture description here

Problem-solving ideas:

In fact, the difficulty of this question lies in the output part. The loop control type is not a big problem. The
output is first to occupy 6 digits, and then it is right-aligned. Here we use it %6.1f, where the 6 before the decimal point represents the occupied width, and the default is Right-aligned, if the back is left-aligned, just add 6 in front of -it!

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114389450