Convert Temperature from Fahrenheit to Celsius and Kelvin in C
1. Introduction
Temperature conversion is a common beginner-level C programming exercise. It helps students understand formulas, input/output operations, and basic arithmetic in C.
2. Problem Statement
Question: Write a C program to accept temperatures in Fahrenheit and print them in Celsius and Kelvin. Use the formulas:
[
C = \frac{5}{9} \times (F – 32), \quad K = C + 273.15
]
3. Logic
To convert Fahrenheit to Celsius and Kelvin:
- Accept the temperature in Fahrenheit from the user.
- Apply the formula (C = \frac{5}{9} \times (F – 32)) to get Celsius.
- Add 273.15 to Celsius to get Kelvin.
- Display both Celsius and Kelvin values.
4. Source Code
Here is a beginner-friendly C program:
#include <stdio.h>
int main() {
float fahrenheit, celsius, kelvin;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
// Convert to Celsius
celsius = (5.0 / 9) * (fahrenheit - 32);
// Convert to Kelvin
kelvin = celsius + 273.15;
printf("Temperature in Celsius: %.2f C\n", celsius);
printf("Temperature in Kelvin: %.2f K\n", kelvin);
return 0;
}
5. Sample Output
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37.00 C
Temperature in Kelvin: 310.15 K
Enter temperature in Fahrenheit: 32
Temperature in Celsius: 0.00 C
Temperature in Kelvin: 273.15 K
6. Viva Questions
- Q: What formula is used to convert Fahrenheit to Celsius?
A: (C = \frac{5}{9} \times (F – 32)) - Q: How do you convert Celsius to Kelvin?
A: (K = C + 273.15) - Q: Can this program handle decimal Fahrenheit values?
A: Yes, usingfloatdata type. - Q: What C function is used to read input from the user?
A:scanf()function. - Q: Why do we use
5.0/9instead of5/9?
A: To perform floating-point division;5/9would perform integer division and give incorrect results.
