Home C Programming Language / 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.

👁 737 Views
💻 Practical Program
📘 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.

No previous program
No next program

📌 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;
}
                        

🖥 Program 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.

No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.