Temperature Conversion in C: Fahrenheit to Celsius and Vice Versa
When it comes to weather, understanding temperature in different units is crucial. In this blog post, we'll explore a simple C program that allows you to convert temperatures between Fahrenheit and Celsius. Let's dive into the code:
#include <stdio.h>
#include <ctype.h>
int main() {
char unit;
float temp;
printf("\nIs the temperature in (F) or (C)?: ");
scanf("%c", &unit);
unit = toupper(unit);
if (unit == 'C') {
printf("\nEnter the temperature in Celsius: ");
scanf("%f", &temp);
temp = (temp * 9 / 5) + 32;
printf("\nThe temperature in Fahrenheit is: %.1f", temp);
} else if (unit == 'F') {
printf("\nEnter the temperature in Fahrenheit: ");
scanf("%f", &temp);
temp = (temp - 32) * 5 / 9;
printf("\nThe temperature in Celsius is: %.1f", temp);
} else {
printf("\n%c is not a valid unit of measurement", unit);
}
return 0;
}
Copy and paste this C code into your favorite compiler, and you'll be able to convert temperatures effortlessly. Whether you're dealing with Celsius or Fahrenheit, this program has you covered.
Feel free to modify and experiment with the code to enhance your understanding of C programming. Happy coding!