Complementary task for topic: 4
M Nemeth · 2023-08-29 15:21:04.619219'
Functions: Convert Fahrenheit to Celsius
Functions: Convert Fahrenheit to Celsius
Write a C function fahrenheit_to_celsius that takes a temperature in Fahrenheit (a floating-point number) as input and returns the equivalent temperature in Celsius as a floating-point number. Use the formula: Celsius = (Fahrenheit - 32) * 5 / 9.
Hint:
Solution
#include
// Function to convert Fahrenheit to Celsius
float fahrenheit_to_celsius(float fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
int main() {
float fahrenheit;
printf("Enter the temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
float celsius = fahrenheit_to_celsius(fahrenheit);
printf("%.2f degrees Fahrenheit is equal to %.2f degrees Celsius.\n", fahrenheit, celsius);
return 0;
}
Explanation
The fahrenheit_to_celsius function takes the temperature in Fahrenheit as input and returns the equivalent temperature in Celsius using the formula: Celsius = (Fahrenheit - 32) * 5 / 9. In the main function, the user is asked to input the temperature in Fahrenheit, and the fahrenheit_to_celsius function is called to convert and print the equivalent temperature in Celsius.