The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters.

C Programming Language / Operators and Enums in C Language

714

Given Input:

Enter the distance between two cities in km: 2

Expected Output:

Distance in meters = 2000.00 m
Distance in feet = 6561.68 ft
Distance in inches = 78740.20 in
Distance in centimeters = 200000.00 cm

Program:

#include<stdio.h>
int main()
{
    float km, m, ft, inch, cm;
    printf("Enter the distance between two cities in km: ");
    scanf("%f", &km);
    m = km * 1000;
    ft = km * 3280.84;
    inch = km * 39370.1;
    cm = km * 100000;
    printf("Distance in meters = %.2f m\n", m);
    printf("Distance in feet = %.2f ft\n", ft);
    printf("Distance in inches = %.2f in\n", inch);
    printf("Distance in centimeters = %.2f cm\n", cm);
    return 0;
}

Output:

Enter the distance between two cities in km: 4
Distance in meters = 4000.00 m
Distance in feet = 13123.36 ft
Distance in inches = 157480.41 in
Distance in centimeters = 400000.00 cm

Explanation:

This code is a simple program that converts kilometers to other units of length such as meters, feet, inches, and centimeters. The program uses the stdio.h library and the scanf() function to take user input for the distance in kilometers. The input is stored in the variable km.

Then, the program uses mathematical formulas to convert the distance in kilometers to other units of length. The formulas are as follows:
m = km * 1000;
ft = km * 3280.84;
inch = km * 39370.1;
cm = km * 100000;

Finally, the program uses the printf() function to print the converted distances in their respective units. The program uses the format specifier %.2f to round off the values to 2 decimal places. The program returns 0 after all the calculations are done, indicating that the program executed successfully.


This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.