FYBCom(CA) C Practical Slip 5 Q 1.A

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:

  1. Accept the temperature in Fahrenheit from the user.
  2. Apply the formula (C = \frac{5}{9} \times (F – 32)) to get Celsius.
  3. Add 273.15 to Celsius to get Kelvin.
  4. 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

  1. Q: What formula is used to convert Fahrenheit to Celsius?
    A: (C = \frac{5}{9} \times (F – 32))
  2. Q: How do you convert Celsius to Kelvin?
    A: (K = C + 273.15)
  3. Q: Can this program handle decimal Fahrenheit values?
    A: Yes, using float data type.
  4. Q: What C function is used to read input from the user?
    A: scanf() function.
  5. Q: Why do we use 5.0/9 instead of 5/9?
    A: To perform floating-point division; 5/9 would perform integer division and give incorrect results.
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top