C program to convert centimeter to meter and kilometer

Write a C program to input length in centimeter from user 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
Output length in kilometer = 0.01 km

Required knowledge:

Fundamentals of C, Data types, Taking user input in C, Length conversion rule

Length conversion table

1 Meter = 100 Centimeter 
1 Kilometer = 100000 Centimeter 

Program

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * C program to convert centimeter into meter and kilometer
 */
 
#include <stdio.h>
 
void 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("\nLength in Meter = %.2f m\n", meter);
    printf("Length in Kilometer = %.2f km", km);
 
}


Output
X
_
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.

Enjoy it.................. =)) 

Comments

Popular posts from this blog

C program to calculate velocity

C program to find square root of a number

C program to find maximum between two numbers