Distance conversion in C programming

Conversion Formulas

Kilometers to Miles

miles=kilometers×0.621371

Miles to Kilometers

kilometers=miles×1.60934

Meters to Feet

feet=meters×3.28084

Feet to Meters

meters=feet×0.3048

C
// Visit: pickupmyskills.com

#include <stdio.h>

int main() {
    int choice;
    double distance, convertedDistance; 

    printf("Distance Conversion\n");
    printf("1. Kilometers to Miles\n");
    printf("2. Miles to Kilometers\n");
    printf("3. Meters to Feet\n");
    printf("4. Feet to Meters\n");
    printf("Enter your choice (1-4): ");
    scanf("%d", &choice);

    if (choice == 1) {
        printf("Enter distance in Kilometers: ");
        scanf("%lf", &distance); 
        convertedDistance = distance * 0.621371;
        printf("Distance in Miles: %.2f\n", convertedDistance);
    } 
    else if (choice == 2) {
        printf("Enter distance in Miles: ");
        scanf("%lf", &distance); 
        convertedDistance = distance * 1.60934;
        printf("Distance in Kilometers: %.2f\n", convertedDistance);
    } 
    else if (choice == 3) {
        printf("Enter distance in Meters: ");
        scanf("%lf", &distance);
        convertedDistance = distance * 3.28084;
        printf("Distance in Feet: %.2f\n", convertedDistance);
    } 
    else if (choice == 4) {
        printf("Enter distance in Feet: ");
        scanf("%lf", &distance); 
        convertedDistance = distance * 0.3048;
        printf("Distance in Meters: %.2f\n", convertedDistance);
    } 
    else {
        printf("Invalid choice! Please enter a number between 1 and 4.\n");
    }

    return 0;
}

Output:

Distance Conversion

  1. Kilometers to Miles
  2. Miles to Kilometers
  3. Meters to Feet
  4. Feet to Meters
    Enter your choice (1-4): 1
    Enter distance in Kilometers: 2
    Distance in Mile 1.24

Leave a Comment

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

Scroll to Top