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

Topic
Input 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 each increment of 2 degrees Fahrenheit.
The calculation formula of temperature conversion: C=5×(F−32)/9, where: C means Celsius temperature, F means Fahrenheit temperature.

Input format:
Enter 2 integers in one line, representing the values ​​of lower and upper respectively, 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 a width of 6 characters, aligns to the right, and retains 1 decimal place).
If the input range is invalid, "Invalid." will be 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.
Correct code

#include <stdio.h>
int main()
{
    
    
 int lower,upper;
 float celsius;
 scanf("%d %d",&lower,&upper);
 if(lower>upper||lower>100||upper>100)
  printf("Invalid.");
 else
 {
    
    
  printf("fahr celsius\n"); 
  for(lower;lower<=upper;lower+=2)
  {
    
    
   printf("%d",lower);
   celsius=5*(lower-32.0)/9;
   printf("%6.1f\n",celsius);
  }
 }
 return 0;
} 

Summarize

  1. If you want to calculate the result as a floating point type, the simple method is to directly change an integer such as 32 into 32.0 in the original calculation formula

  • "-" - left-aligned output, the default is right-aligned output;

  • "+"——positive number output plus sign (+), negative number output minus sign (-);

  • Space—a positive number outputs a space instead of a plus sign (+), and a negative number outputs a minus sign (-);

  • Use a decimal positive integer to represent the minimum number of characters for setting the output value.

  • If it is insufficient, fill in the blank space, if it is too much, it will be output according to the actual value, and the default value will be output according to the actual value.

    例如:
    printf("%8d\n",100); └┘└┘└┘└┘└┘100
    printf("%6d\n",100); └┘└┘└┘100
    printf("%-8d\n",100); 100└┘└┘└┘└┘└┘
    printf("%+8\n",100);└┘└┘└┘└┘+100

insert image description here
At the beginning, the error refers to the fact that lower<=upper did not write an equal sign.

Guess you like

Origin blog.csdn.net/qq_45191169/article/details/107375862