Convert distance in km to meters, feets, inches and centimeters
The distance between two cities (in km.) is input through the keyboard. Write a program in C to convert and print this distance in meters, feet, inches and centimeters.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
double km;
printf("Enter the distance in kilo meters\t:");
scanf("%lf",&km);
printf("\nDistance in Meters is = %lf",1000*km);
printf("\nDistance in Feet is = %lf",3280.84*km);
printf("\nDistance in inches is = %lf",39370.08*km);
printf("\nDistance in Centi Meters is = %lf",100000*km);
getch();
}
Output
Enter the distance in kilo meters :4
Distance in Meters is = 4000.000000
Distance in Feet is = 13123.360000
Distance in inches is = 157480.320000
Distance in Centi Meters is = 400000.000000
Explanation
In this program, distance is taken as an input from the user in variable named 'km'. %lf is the format specifier for double data type.
conversion formulas from kilometer to meters, feet, inches and centimeters are as follows:
Instead of storing the result in variables and then printing its value, calculations are done inside printf() statement itself and result is printed.
Here, first of all, calculation is performed and is replaced by the value obtained. And then, that value is printed.
conversion formulas from kilometer to meters, feet, inches and centimeters are as follows:
meter = 1000 * km
feet = 3280.84 * km
inches = 39370.08 * km
centimeter = 100000 * km
Instead of storing the result in variables and then printing its value, calculations are done inside printf() statement itself and result is printed.
printf("\nDistance in Meters is = %lf",1000*km);
printf("\nDistance in Feet is = %lf",3280.84*km);
printf("\nDistance in inches is = %lf",39370.08*km);
printf("\nDistance in Centi Meters is = %lf",100000*km);
Here, first of all, calculation is performed and is replaced by the value obtained. And then, that value is printed.