1. Introduction
Worldwide, temperatures are often measured using either the Celsius or Fahrenheit scales. While many countries use the Celsius scale, the United States predominantly uses Fahrenheit. Thus, having a tool that can easily convert between these scales can be invaluable. In this guide, we will devise a C program that undertakes the conversion of a temperature from Fahrenheit to Celsius.
2. Program Overview
Our program will perform the following tasks:
1. Prompt the user to input a temperature in Fahrenheit.
2. Calculate the equivalent Celsius value using the relevant conversion formula.
3. Display the converted temperature to the user.
3. Code Program
#include <stdio.h> // Include the Standard I/O library
int main() { // Commence the main program
float fahrenheit, celsius; // Declare float variables for the temperature readings
// Prompt the user to input a temperature in Fahrenheit
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit); // Record the Fahrenheit temperature
// Deploy the conversion formula to calculate the Celsius temperature
celsius = (fahrenheit - 32) * 5/9;
printf("The temperature in Celsius is: %.2f\n", celsius);
return 0; // Sign off from the program gracefully
}
Output:
Enter temperature in Fahrenheit: 77 The temperature in Celsius is: 25.00
4. Step By Step Explanation
1. #include <stdio.h>: This command incorporates the standard input and output library, granting us the liberty to employ functions like printf and scanf.
2. int main(): This is the primary function from which our program gets executed.
3. Variable Declaration:
- fahrenheit and celsius: These float variables are designed to store the input and the derived temperatures, respectively.
4. User Input:
- We kindly request the user to offer a temperature value in Fahrenheit.
- Utilizing the scanf function, we capture and archive the user's input inside the fahrenheit variable.
5. Conversion to Celsius:
- The formula to effect the transformation from Fahrenheit to Celsius is represented by: C = (F - 32) * 5/9, with C symbolizing the temperature in Celsius and F representing the Fahrenheit temperature.
- Our code leverages this formula to discern the Celsius equivalent and keeps the outcome in the celsius variable.
6. Display the Result: We then showcase the deduced Celsius temperature to the user.
With the assistance of this compact conversion formula, our program becomes a bridge, facilitating a seamless transition from Fahrenheit to Celsius, and vice-versa.
Comments
Post a Comment
Leave Comment