Home / Programs / 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.
Programming Example

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.

👁 734 Views
💻 Practical Program
📘 Step by Step Learning

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.

Information & Algorithm

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 Code

#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.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.