Write a program in C to convert distance from kilometer to meter, centimeter and millimeter.
WAP in C to convert kilometer(km) to meter(m).
Required Knowledge
- C printf and scanf functions
- C Arithmetic Operators
We will first take distance in kilometer(km) as input from user using scanf function and then convert it to meter, centimeter and millimeter using following formulae.
- 1 Kilometer = 1000 Meter
- 1 Kilometer = 100000 Centimeter
- 1 Kilometer = 1000000 Millimeter
C program to convert kilometer to meter, centimeter and millimeter
/** * C program to convert kilometer into meter, * centimeter and millimeter */ #include <stdio.h> int main() { float meter, centimeter, millimeter, kilometer; /* * Take length in Kilimeter as input form user */ printf("Enter Length in Kilometer(km)\n"); scanf("%f", &kilometer); /* * 1 km = 1000 m, 1 km = 100000 cm, 1 km = 1000000 mm */ meter = kilometer * 1000; centimeter = kilometer * 100000; millimeter = kilometer * 1000000; printf("%.4f Kilometer = %.4f Meter\n", kilometer, meter); printf("%.4f Kilometer = %.4f Centimeter\n",kilometer,centimeter); printf("%.4f Kilometer = %.4f Millimeter\n",kilometer,millimeter); return 0; }
Output
Enter Length in Kilometer(km) 4.0 4.0000 Kilometer = 4000.0000 Meter 4.0000 Kilometer = 400000.0000 Centimeter 4.0000 Kilometer = 4000000.0000 Millimeter