Menu-Driven Unit Converter Using Switch Statement in C

This program implements a menu-driven unit converter (kilometers to miles, Celsius to Fahrenheit) using a switch statement.

C Program

#include <stdio.h>

int main() {
    int choice;
    float value, result;

    printf("Unit Converter Menu\n");
    printf("1. Kilometers to Miles\n");
    printf("2. Celsius to Fahrenheit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Enter distance in kilometers: ");
            scanf("%f", &value);
            result = value * 0.621371;
            printf("%.2f km = %.2f miles\n", value, result);
            break;
        case 2:
            printf("Enter temperature in Celsius: ");
            scanf("%f", &value);
            result = (value * 9.0 / 5.0) + 32;
            printf("%.2f C = %.2f F\n", value, result);
            break;
        default:
            printf("Invalid choice.\n");
    }

    return 0;
}

Explanation

  • The menu prints available options, and switch (choice) branches to the matching conversion.
  • Kilometers to miles multiplies by the conversion factor 0.621371.
  • Celsius to Fahrenheit applies the standard formula F = C * 9/5 + 32; using 9.0 and 5.0 (not 9 and 5) forces floating-point division so the ratio isn’t truncated to 1.
  • An invalid menu choice falls through to default.

Sample Output

Unit Converter Menu
1. Kilometers to Miles
2. Celsius to Fahrenheit
Enter your choice: 2
Enter temperature in Celsius: 100
100.00 C = 212.00 F

Leave a Comment