Create a Temperature Converter in C Language with Free Codes

Tem Converter in C

Table of Contents

Introduction – Temperature Converter

Here we create a simple and basic console base Project Temperature Converter in C Language. Which is used for boost the skills and logic knowledge about the C Language.

Feature of the Project

Here is the feature of the Project,

  1. Celsius to Fahrenheit
  2. Fahrenheit to Celsius

Code Explanation and Breakdown

Here is the explanation about the Project Code – Temperature Converter.

Part -1 Including Standard Libraries

#include <stdio.h>
  • <stdio.h>: Here we provides input/output functions like printf() and scanf().

Part – 2 Temperature Conversion Functions

1. ‘celsiusToFahrenheit()’

It is used for converts temperature from Celsius to Fahrenheit. And it Uses the formula (Celsius * 9/5) + 32 to convert Celsius to Fahrenheit.

Free Notes
double celsiusToFahrenheit(double celsius) {
    return (celsius * 9 / 5) + 32;
}
2. ‘fahrenheitToCelsius()’

It is used for converts temperature from Fahrenheit to Celsius. And it Uses the formula (Fahrenheit - 32) * 5/9 to convert Fahrenheit to Celsius.

Part 3 – Main Function

int main() {
    int choice;
    double temperature;

    printf("Temperature Converter\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 && choice != 2) {
        printf("Invalid choice. Please enter 1 or 2.\n");
        return 1; // Exit with error status
    }

    printf("Enter the temperature: ");
    scanf("%lf", &temperature);

    switch (choice) {
        case 1:
            printf("%.2f Celsius is equivalent to %.2f Fahrenheit.\n",
                   temperature, celsiusToFahrenheit(temperature));
            break;
        case 2:
            printf("%.2f Fahrenheit is equivalent to %.2f Celsius.\n",
                   temperature, fahrenheitToCelsius(temperature));
            break;
    }

    return 0;
}
  • Here Main Function will display a menu for choosing the type of temperature conversion (Celsius to Fahrenheit or Fahrenheit to Celsius).
  • After Display it reads the user’s choice (1 or 2) using scanf().
  • Then it validates the user’s choice and shows the prompts for temperature input.
  • Uses a switch statement to perform the selected temperature conversion based on the user’s choice.
  • Prints the converted temperature with two decimal places using printf().

Complete Project Codes

Here is the complete codes of the project,

#include <stdio.h>

// Function to convert Celsius to Fahrenheit
double celsiusToFahrenheit(double celsius) {
    return (celsius * 9 / 5) + 32;
}

// Function to convert Fahrenheit to Celsius
double fahrenheitToCelsius(double fahrenheit) {
    return (fahrenheit - 32) * 5 / 9;
}

int main() {
    int choice;
    double temperature;

    printf("Temperature Converter\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 && choice != 2) {
        printf("Invalid choice. Please enter 1 or 2.\n");
        return 1; // Exit with error status
    }

    printf("Enter the temperature: ");
    scanf("%lf", &temperature);

    switch (choice) {
        case 1:
            printf("%.2f Celsius is equivalent to %.2f Fahrenheit.\n",
                   temperature, celsiusToFahrenheit(temperature));
            break;
        case 2:
            printf("%.2f Fahrenheit is equivalent to %.2f Celsius.\n",
                   temperature, fahrenheitToCelsius(temperature));
            break;
    }

    return 0;
}

Conclusion

Here we Learn about how we create a logically function for Temperature Converter with the help of Math calculation or Math Functions.

Leave a Comment

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

Scroll to Top