(Temperature Conversions) Write a program that converts integer Fahrenheit tempe
ID: 3735430 • Letter: #
Question
(Temperature Conversions) Write a program that converts integer Fahrenheit temperatures from 0 to 212 degrees to floating-point Celsius temperatures with 3 digits of precision. Perform the calculation using the formula
The output should be printed in two right-justified columns of 10 characters each, and the Celsius temperatures should be preceded by a sign for both positive and negative values.
#include <stdio.h>
int main()
{
//Store temp in Fahrenheit
int temp_fahrenheit;
//store temp in Celsius
float temp_celsius;
printf(" Temperature conversion from Fahrenheit to Celsius is given below: ");
printf(" %10s %12s ", "Fahrenheit", "Celsius");
//For loop to convert
for (temp_fahrenheit = 0; temp_fahrenheit <= 212; temp_fahrenheit++)
{
temp_celsius = 5.0 / 9.0 *(temp_fahrenheit - 32);
printf("%10d %12.3f ", temp_fahrenheit, temp_celsius);
}// End loop
return 0;
}
This is my code and it works, but I am looking specifically for different ways to write this code or is this the only way?
Explanation / Answer
Your's code is working correctly and this is the good way to find the required output. But as you want different way to implement this code, then you can use while loop in place of for, output will remain same.
Code with while loop is as follows:
#include <stdio.h>
int main()
{
//Store temp in Fahrenheit
int temp_fahrenheit;
//store temp in Celsius
float temp_celsius;
printf(" Temperature conversion from Fahrenheit to Celsius is given below: ");
printf(" %10s %12s ", "Fahrenheit", "Celsius");
//For loop to convert
temp_fahrenheit=0;
while(temp_fahrenheit <= 212)
{
temp_celsius = 5.0 / 9.0 *(temp_fahrenheit - 32);
printf("%10d %12.3f ", temp_fahrenheit, temp_celsius);
temp_fahrenheit++;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.