Temperature conversion is a fundamental concept in physics and everyday life, especially when dealing with weather reports, scientific calculations, or international measurements. In many cases, temperatures need to be converted from Celsius (°C) to Fahrenheit (°F) or vice versa.
This simple program will allow users to convert temperatures between these two units, making it useful for a wide range of applications.
Objective:
The objective of this program is to provide a simple tool that can convert temperatures between Celsius and Fahrenheit. The program will prompt the user to input a temperature value, select the conversion direction, and display the result. This tool can be helpful for students, travelers, scientists, and engineers who need to work with temperature data in different units.
Program Code:
#include int main() { float temperature, convertedTemperature; int choice; // Display menu printf("Temperature Converter Program\n"); printf("Select the conversion type:\n"); printf("1. Celsius to Fahrenheit\n"); printf("2. Fahrenheit to Celsius\n"); printf("Enter your choice (1 or 2): "); scanf("%d", &choice); if (choice == 1) { // Celsius to Fahrenheit conversion printf("Enter temperature in Celsius: "); scanf("%f", &temperature); convertedTemperature = (temperature * 9/5) + 32; printf("%.2f Celsius is equal to %.2f Fahrenheit.\n", temperature, convertedTemperature); } else if (choice == 2) { // Fahrenheit to Celsius conversion printf("Enter temperature in Fahrenheit: "); scanf("%f", &temperature); convertedTemperature = (temperature - 32) * 5/9; printf("%.2f Fahrenheit is equal to %.2f Celsius.\n", temperature, convertedTemperature); } else { printf("Invalid choice! Please enter either 1 or 2.\n"); } return 0; }
Program Structure and Explanation:
This C program follows a simple structure with the following key components:
- Header Files: We include the standard input-output library using
#include <stdio.h>
to enable basic input and output functionality. - Variable Declaration: We declare two variables:
temperature
to store the user’s input temperature, andconvertedTemperature
to store the converted result. An integerchoice
is also declared to hold the user’s choice for conversion. - Menu Display: The program displays a menu asking the user whether they want to convert from Celsius to Fahrenheit or vice versa. The user selects an option by entering
1
or2
. - Conversion Logic: Based on the user’s choice, the program performs the appropriate conversion:
- If the user selects 1 (Celsius to Fahrenheit), the program applies the formula
F = (C * 9/5) + 32
. - If the user selects 2 (Fahrenheit to Celsius), the program applies the formula
C = (F - 32) * 5/9
.
- If the user selects 1 (Celsius to Fahrenheit), the program applies the formula
- Error Handling: If the user enters a choice other than 1 or 2, the program will display an error message.
- Output: The program outputs the converted temperature in the desired format, rounded to two decimal places.
How to Run the Program:
To run this program, follow these steps:
- Write the code in a text editor and save it as
temp_converter.c
. - Open a terminal or command prompt.
- Navigate to the directory where the
temp_converter.c
file is saved. - Compile the program using a C compiler (e.g., GCC) with the following command:
gcc temp_converter.c -o temp_converter
- Run the compiled program by entering the following command:
./temp_converter
- Follow the on-screen instructions to select the conversion type and input the temperature.
Conclusion:
This program serves as a basic utility for converting temperatures between Celsius and Fahrenheit. By understanding the code and the conversion formulas, users can easily modify or extend the program for other temperature-related tasks. Whether for educational purposes or daily use, this simple tool can save time and improve accuracy when working with temperature data.