Program to convert a temperature from Celsius to Fahrenheit using a union

C Programming Language / Union in C Language

248

Given Input:

Enter temperature in Celsius: -40

Expected Output:

Temperature in Fahrenheit = -40.00

Program:

#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;
}

Output:

Enter temperature in Celsius: -40
Temperature in Fahrenheit = -40.00

Explanation:

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.


This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.