Header-C
Header-C

 

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, and convertedTemperature to store the converted result. An integer choice 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 or 2.
  • 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.
  • 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:

  1. Write the code in a text editor and save it as temp_converter.c.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the temp_converter.c file is saved.
  4. Compile the program using a C compiler (e.g., GCC) with the following command:
    gcc temp_converter.c -o temp_converter
  5. Run the compiled program by entering the following command:
    ./temp_converter
  6. 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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)