C program to convert centimeter to meter and kilometer

CServer Side ProgrammingProgramming

Here, the user has to enter the length in centimeters (cm), and then, convert the length into meters (m), and kilometer (km).

1 Meter = 100 Centimeters
1 Kilometer = 100000 Centimeters

Algorithm

Refer an algorithm given below to convert centimeter into meter and kilometer respectively.

Step 1: Declare variables.
Step 2: Enter length in cm at runtime.
Step 3: Compute meter by using the formula given below.
   meter = centim / 100.0;
Step 4: Compute kilometer by using the formula given below.
   kilometer = centim / 100000.0
Step 5: Print meter.
Step 6: Print kilomete

Example1

Following is the C program to convert centimeter to meter and kilometer respectively −

 Live Demo

#include <stdio.h>
int main(){
   float centim, meter, kilometer;
   printf("enter length in cm:\n ");
   scanf("%f", &centim);
   meter = centim / 100.0;
   kilometer = centim / 100000.0;
   printf("length in mtrs = %.4f\n", meter);
   printf("length in km = %.4f", kilometer);
   return 0;
}

Output

When the above program is executed, it produces the following result −

enter length in cm:
length in mtrs = 0.0000
length in km = 0.0000

Example2

Following is the C Program to convert meter to centimeter and kilometer respectively.

 Live Demo

#include <stdio.h>
int main(){
   float centim, meter, kilometer;
   printf("enter length in meter:\n ");
   scanf("%f", &meter);
   centim = meter*100.0;
   kilometer = meter / 1000.0;
   printf("length in cm = %.4f\n", centim);
   printf("length in km = %.4f", kilometer);
   return 0;
}

Output

When the above program is executed, it produces the following result −

enter length in meter:
length in cm = 0.0000
length in km = 0.0000
raja
Published on 24-Mar-2021 17:33:11
Advertisements