C program to convert kilometer to cm, meter, feet and mile
C Program to convert kilometer to meter, cm, feet & mile:
Write a C program to to input distance in kilometer from user and convert it to meter, cm, feet, and mile. Let's have a look at the below example with step by step logic to convert km to meter, cm, feet, and mile.
Before writing any program you just imagine about output screen first. Like what input to be given, and output to be displayed. Let's have a look at the below example.
- INPUT
- Enter distance [in kilometer] = 1
OUTPUT
1 kilometers = 1000.00 m
1 kilometers = 100000.00 cm
1 kilometers = 3280.84 feets
1 kilometers = 0.6214 miles
Required knowledge for this exercise is:
Logic to convert km to meter, cm, feet and mile:
1) Print a massage to the user like "Enter distance [in kilometer]:" using printf();
2) Input distance [in kilometer] from user and store it in a variable km using scanf();
3) Now convert km to meter, cm, feet & mile using below farmula
meter = km * 1000.0; // since, 1km = 1000 m
cm = km * 1000.0 * 100.0 // since, 1 km = 100000 cm
feet = km * 3280.84 // since, 1 km = 3280.84 feets
mile = km * 0.6214 // since, 1 km = 0.621371 miles
4) At last print the value of meter, cm, feet and mile. that's it!
Program to convert kilometer to meter, cm, feet & mile:
-
#include <stdio.h>
- int main()
{
float km, meter, cm, feet, mile;
printf("Enter distance [in Kilometers]: ");
scanf("%f", &km);
meter = km * 1000.0; // since, 1km = 1000 m
- cm = km * 1000.0 * 100.0 // since, 1 km = 100000 cm
- feet = km * 3280.84 // since, 1 km = 3280.84 feets
mile = km * 0.6214 // since, 1 km = 0.621371 miles
-
printf("%.2f Kilometers = %.2f m", km, meter);
- printf("%.2f Kilometers = %.2f cm", km, cm);
- printf("%.2f Kilometers = %.2f feets", km, feet);
- printf("%.2f Kilometers = %.2f miles", km, mile);
-
return 0;
}
%.2f
is used to print real values only up to 2 decimal places. You can also use %f
to print up to 6 decimal places by default.
Output:
- Enter distance [in Kilometers] = 1
- 1 kilometers = 1000.00 m
- 1 kilometers = 100000.00 cm
- 1 kilometers = 3280.84 feets
- 1 kilometers = 0.62 miles
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.