C program to convert centimeter to meter and kilometer

Previous Program Next Program

Write a C program to input length in centimeter and convert it to meter and kilometer. How to convert length from centimeter to meter and kilometer in C programming. Length conversion program in C from centimeter to meter and centimeter to kilometer.

Example

Input

Length in centimeter = 1000

Output

Length in meter = 10 m
Length in kilometer = 0.01 km

Required knowledge

Fundamentals of C, Data types, Talking user input in C

Length conversion formula

Centimeter to meter and centimeter to kilometer conversion table is given by -

Centimeter to meter and kilometer conversion table

Program to convert centimeter to meter and kilometer

/**
 * C program to convert centimeter into meter and kilometer
 */

#include <stdio.h>

int main()
{
    float cm, meter, km;

    // Read length in centimeter from user
    printf("Enter length in centimeter: ");
    scanf("%f", &cm);

    // Convert centimeter into meter and kilometer
    meter = cm / 100.0;
    km = cm / 100000.0;

    printf("Length in Meter = %.2f m\n", meter);
    printf("Length in Kilometer = %.2f km", km);

    return 0;
} 
Output
Enter length in centimeter: 1000
Length in Meter = 10.00 m
Length in Kilometer = 0.01 km

Note: %.2f is used to print the fractional value up to two decimal places. You can also use %f to print by default up to 6 decimal places.

Happy coding ;)

Recommended posts

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

1 comment: