Write a C program to convert centimeters to meters and kilometers
Description: Take a length in centimeters and print it in meters and kilometers.
Hint:
- 1 meter = 100 cm and 1 kilometer = 100000 cm.
C program to convert centimeters to meters and kilometers
#include <stdio.h>
int main(void) {
float cm, meter, km;
printf("Enter length in cm: ");
scanf("%f", &cm);
meter = cm / 100;
km = cm / 100000;
printf("Meters = %.2f\n", meter);
printf("Kilometers = %.5f\n", km);
return 0;
}
Output:
Output of the above program is
Enter length in cm: 25000
Meters = 250.00
Kilometers = 0.25000
How it works
- We divide the centimeters by 100 to get meters, and by 100000 to get kilometers.
- The variables are float, so the decimal part is not lost.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |