In this article, you will learn how to convert a temperature from Fahrenheit to Celsius and vice versa. The concept is easy: If the user selects to convert from Fahrenheit to Celcius, then the program should subtract 32 from the original temperature, then divide the result by 1.8.
On the other hand, if the user selects to convert from Celsius to Fahrenheit, we multiply the original value by 1.8 and add 32 to it.
#include <stdio.h> int main() { float fahrenheit, celsius; int choice; while (choice != 3) { printf("\n1: Convert temperature from Fahrenheit to Celsius."); printf("\n2: Convert temperature from Celsius to Fahrenheit."); printf("\n3: Exit."); printf("\nEnter your choice: "); scanf("%d", & choice); switch (choice) { case 1: { printf("\nPlease enter temperature in Fahrenheit: "); scanf("%f", & fahrenheit); celsius = (fahrenheit - 32) / 1.8; printf("Temperature in Celsius is: %.2f", celsius); break; } case 2: { printf("\nPlease enter temperature in Celsius: "); scanf("%f", & celsius); fahrenheit = (celsius * 1.8) + 32; printf("Temperature in Fahrenheit is: %.2f", fahrenheit); } } } return 0; }
Output
1: Convert temperature from Fahrenheit to Celsius. 2: Convert temperature from Celsius to Fahrenheit. 3: Exit. Enter your choice: 2 Please enter temperature in Celsius: 25 Temperature in Fahrenheit is: 77.00
Happy Coding!