C Programming: Temperature Converter Tutorial – Celsius, Fahrenheit, Kelvin

Temperature conversion is a fundamental programming exercise that teaches essential C programming concepts. In this tutorial, we’ll build a comprehensive temperature converter that handles Celsius, Fahrenheit, and Kelvin conversions with proper input validation and user-friendly interface.

Learning Objectives

  • Understanding mathematical operations in C
  • Working with floating-point arithmetic
  • Implementing menu-driven programs
  • Input validation and error handling
  • Creating reusable functions

Basic Temperature Converter

#include <stdio.h>

int main() {
    float celsius, fahrenheit, kelvin;
    int choice;
    
    printf("==========================================\n");
    printf("       TEMPERATURE CONVERTER PROGRAM     \n");
    printf("==========================================\n\n");
    
    printf("Select conversion type:\n");
    printf("1. Celsius to Fahrenheit\n");
    printf("2. Fahrenheit to Celsius\n");
    printf("3. Celsius to Kelvin\n");
    printf("4. Kelvin to Celsius\n");
    printf("5. Fahrenheit to Kelvin\n");
    printf("6. Kelvin to Fahrenheit\n");
    printf("Enter your choice (1-6): ");
    scanf("%d", &choice);
    
    switch(choice) {
        case 1:
            printf("Enter temperature in Celsius: ");
            scanf("%f", &celsius);
            fahrenheit = (celsius * 9.0/5.0) + 32;
            printf("%.2f°C = %.2f°F\n", celsius, fahrenheit);
            break;
            
        case 2:
            printf("Enter temperature in Fahrenheit: ");
            scanf("%f", &fahrenheit);
            celsius = (fahrenheit - 32) * 5.0/9.0;
            printf("%.2f°F = %.2f°C\n", fahrenheit, celsius);
            break;
            
        case 3:
            printf("Enter temperature in Celsius: ");
            scanf("%f", &celsius);
            kelvin = celsius + 273.15;
            printf("%.2f°C = %.2fK\n", celsius, kelvin);
            break;
            
        case 4:
            printf("Enter temperature in Kelvin: ");
            scanf("%f", &kelvin);
            if (kelvin < 0) {
                printf("Error: Temperature cannot be below absolute zero!\n");
            } else {
                celsius = kelvin - 273.15;
                printf("%.2fK = %.2f°C\n", kelvin, celsius);
            }
            break;
            
        case 5:
            printf("Enter temperature in Fahrenheit: ");
            scanf("%f", &fahrenheit);
            kelvin = (fahrenheit - 32) * 5.0/9.0 + 273.15;
            printf("%.2f°F = %.2fK\n", fahrenheit, kelvin);
            break;
            
        case 6:
            printf("Enter temperature in Kelvin: ");
            scanf("%f", &kelvin);
            if (kelvin < 0) {
                printf("Error: Temperature cannot be below absolute zero!\n");
            } else {
                fahrenheit = (kelvin - 273.15) * 9.0/5.0 + 32;
                printf("%.2fK = %.2f°F\n", kelvin, fahrenheit);
            }
            break;
            
        default:
            printf("Invalid choice! Please run the program again.\n");
            break;
    }
    
    return 0;
}

Advanced Temperature Converter with Functions

#include <stdio.h>
#include <stdlib.h>

// Function prototypes
float celsiusToFahrenheit(float celsius);
float fahrenheitToCelsius(float fahrenheit);
float celsiusToKelvin(float celsius);
float kelvinToCelsius(float kelvin);
float fahrenheitToKelvin(float fahrenheit);
float kelvinToFahrenheit(float kelvin);
void displayMenu();
int validateKelvin(float kelvin);

int main() {
    float temperature;
    int choice;
    char continueChoice;
    
    do {
        printf("==========================================\n");
        printf("     ADVANCED TEMPERATURE CONVERTER      \n");
        printf("==========================================\n\n");
        
        displayMenu();
        printf("Enter your choice (1-7): ");
        scanf("%d", &choice);
        
        if (choice == 7) {
            printf("Thank you for using Temperature Converter!\n");
            break;
        }
        
        if (choice < 1 || choice > 7) {
            printf("Invalid choice! Please try again.\n");
            continue;
        }
        
        switch(choice) {
            case 1:
                printf("Enter temperature in Celsius: ");
                scanf("%f", &temperature);
                printf("%.2f°C = %.2f°F\n", temperature, celsiusToFahrenheit(temperature));
                break;
                
            case 2:
                printf("Enter temperature in Fahrenheit: ");
                scanf("%f", &temperature);
                printf("%.2f°F = %.2f°C\n", temperature, fahrenheitToCelsius(temperature));
                break;
                
            case 3:
                printf("Enter temperature in Celsius: ");
                scanf("%f", &temperature);
                printf("%.2f°C = %.2fK\n", temperature, celsiusToKelvin(temperature));
                break;
                
            case 4:
                printf("Enter temperature in Kelvin: ");
                scanf("%f", &temperature);
                if (validateKelvin(temperature)) {
                    printf("%.2fK = %.2f°C\n", temperature, kelvinToCelsius(temperature));
                } else {
                    printf("Error: Temperature cannot be below absolute zero (0K)!\n");
                }
                break;
                
            case 5:
                printf("Enter temperature in Fahrenheit: ");
                scanf("%f", &temperature);
                printf("%.2f°F = %.2fK\n", temperature, fahrenheitToKelvin(temperature));
                break;
                
            case 6:
                printf("Enter temperature in Kelvin: ");
                scanf("%f", &temperature);
                if (validateKelvin(temperature)) {
                    printf("%.2fK = %.2f°F\n", temperature, kelvinToFahrenheit(temperature));
                } else {
                    printf("Error: Temperature cannot be below absolute zero (0K)!\n");
                }
                break;
        }
        
        printf("\nWould you like to perform another conversion? (y/n): ");
        scanf(" %c", &continueChoice);
        
    } while (continueChoice == 'y' || continueChoice == 'Y');
    
    printf("Goodbye!\n");
    return 0;
}

void displayMenu() {
    printf("Select conversion type:\n");
    printf("1. Celsius to Fahrenheit\n");
    printf("2. Fahrenheit to Celsius\n");
    printf("3. Celsius to Kelvin\n");
    printf("4. Kelvin to Celsius\n");
    printf("5. Fahrenheit to Kelvin\n");
    printf("6. Kelvin to Fahrenheit\n");
    printf("7. Exit\n\n");
}

float celsiusToFahrenheit(float celsius) {
    return (celsius * 9.0/5.0) + 32;
}

float fahrenheitToCelsius(float fahrenheit) {
    return (fahrenheit - 32) * 5.0/9.0;
}

float celsiusToKelvin(float celsius) {
    return celsius + 273.15;
}

float kelvinToCelsius(float kelvin) {
    return kelvin - 273.15;
}

float fahrenheitToKelvin(float fahrenheit) {
    return fahrenheitToCelsius(fahrenheit) + 273.15;
}

float kelvinToFahrenheit(float kelvin) {
    return celsiusToFahrenheit(kelvinToCelsius(kelvin));
}

int validateKelvin(float kelvin) {
    return kelvin >= 0;
}

Temperature Conversion Formulas

Celsius to Fahrenheit: F = (C × 9/5) + 32

Fahrenheit to Celsius: C = (F – 32) × 5/9

Celsius to Kelvin: K = C + 273.15

Kelvin to Celsius: C = K – 273.15

Fahrenheit to Kelvin: K = (F – 32) × 5/9 + 273.15

Kelvin to Fahrenheit: F = (K – 273.15) × 9/5 + 32

Key Programming Concepts

Function Design: Each conversion is implemented as a separate function, promoting code reusability and modularity.

Input Validation: The program validates temperature values against absolute zero limits for each scale.

Menu Systems: Interactive menu-driven interface with multiple operation modes.

Error Handling: Comprehensive error checking for invalid inputs and impossible temperature values.

Compilation and Execution

# Compile the program
gcc temperature_converter.c -o temp_converter

# Run the program
./temp_converter

Practice Exercises

  1. Add Rankine temperature scale conversion
  2. Create a batch conversion feature for multiple temperatures
  3. Add historical temperature data comparison
  4. Implement temperature range validation for different scales
  5. Create a file output feature to save conversion results

Conclusion

This temperature converter demonstrates fundamental C programming concepts including mathematical operations, function design, and input validation. The modular approach makes the code maintainable and extensible for future enhancements.

Written by:

191 Posts

View All Posts
Follow Me :
How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site