Program to convert a temperature from Celsius to Fahrenheit using a union
Program to convert a temperature from Celsius to Fahrenheit using a union
Program to convert a temperature from Celsius to Fahrenheit using a union
Enter temperature in Celsius: -40
Temperature in Fahrenheit = -40.00
#include <stdio.h>
union Temperature {
float celsius;
float fahrenheit;
};
int main() {
union Temperature temp;
printf("Enter temperature in Celsius: ");
scanf("%f", &temp.celsius);
temp.fahrenheit = (temp.celsius * 9.0 / 5.0) + 32.0;
printf("Temperature in Fahrenheit = %.2f\n", temp.fahrenheit);
return 0;
}
Enter temperature in Celsius: -40
Temperature in Fahrenheit = -40.00
In this program, we define a union "Temperature" that can store either a temperature in Celsius or a temperature in Fahrenheit. We then use the union to convert a temperature from Celsius to Fahrenheit.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.