In: Computer Science
C language <stdio.h> (functions)
Write a program to display a table of Fahrenheit temperatures and their equivalent Celsius temperatures. Create a function to do the conversion from Fahrenheit to Celsius. Invoke the function from within a for loop in main() to display the table. Ask the user for a starting temperature and and ending temperature. Validate that the starting temperature is within the range of -100 to 300 degrees, Validate that the ending temperature is in the range of -100 to 300 degrees, and is also greater than the starting temperature. Use a decision inside the ending temperature validation loop to display either the "out-of-range" message or the "greater than starting temperature" message.
The formula for converting a Fahrenheit temperature to Celsius
is:
C = (5 / 9) * (F - 32)
Use this exact formula - do not pre-calculate 5/9 as .56 (you will
have to type cast one of the numbers to a double for the equation
to work correctly).
Format the Celsius temperatures to 1 decimal place.
Example Run #1:
(bold type is what is entered by the user)
Enter a starting Fahrenheit temperature:
-200
The starting temperature must be between -100 and 300
degrees.
Please re-enter the starting temperature: 0
Enter an ending Fahrenheit temperature: 500
The ending temperature must be between -100 and 300 degrees.
Please re-enter the ending temperature: -20
The ending temperature, -20, must be greater than the starting
temperature.
Please re-enter the ending temperature: 20
Fahrenheit Celsius
0 xx.x
1 xx.x
2 xx.x
3 xx.x
4 xx.x
5 xx.x
6 xx.x
7 xx.x
8 xx.x
9 xx.x
10 xx.x
11 xx.x
12 xx.x
13 xx.x
14 xx.x
15 xx.x
16 xx.x
17 xx.x
18 xx.x
19 xx.x
20 xx.x
The example run shows EXACTLY how your program input and output will look.
SOURCE CODE
#include <stdio.h>
int main()
{
float c, f1=500, f2=500;
int f;
printf("Enter a starting Fahrenheit temperature: ");
while(f1 < -100 || f1 > 300)
{
scanf("%f", &f1);
if( f1 < -100 || f1 > 300)
{
printf("The starting temperature must be between -100 and 300
degrees.\n");
printf("Please re-enter the starting temperature: ");
}
}
printf("Enter an ending Fahrenheit temperature : ");
while(f2 < -100 || f2 > 300 || f2 < f1)
{
scanf("%f", &f2);
if( f2 < -100 || f2 > 300)
{
printf("The ending temperature must be between -100 and 300
degrees.\n");
printf("Please re-enter the ending temperature: ");
}
if( f1 > f2)
{
printf("The ending temperature, %.0f, must be greater than the
starting temperature.\n",f2);
printf("Please re-enter the ending temperature: ");
}
}
for(f=f1; f<=f2; f++)
{
c = (float)(f - 32) * 5 / 9;
printf("%d %.1f \n", f, c);
}
return 0;
}
SCREENSHOT


please give a upvote if u feel helpful.