Question:
The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in metres, feet, inches and centimetres.Solution to convert km to m,feet,inch and cm in C
This problem is similar to the previous problem, Calculate gross salary in C. Here, we just have to input the distance between two cities in kilometers. Let’s create a variable named “km” and store the value inputted using scanf() function. Then, using respective variables for meters, feet, inch and centimeters, convert the “km” variable into the respective metric and print the output. I have used the variables ‘m’,’feet’,’inch’ and ‘cm’ for meters, feet, inches and centimeters respectively. Then store the formulas into their respective variables and print the output.Download the source code:
Download ProgramProgram to convert km to m,feet,inch and cm in C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include<stdio.h>
#include<conio.h>
void main(){
float km,m,feet,inch,cm;
printf("Enter the distance between two cities(in km) - ");
scanf("%f",&km);
m = km*1000; //since 1km = 1000m
feet= km*3280.84; //since 1km=3280.84feet
inch=km*39370.1; //since 1 km=39370.1inches
cm=km*100000; //since 1km = 100000cm
printf("\nDistance in kilometres = %f ",km);
printf("\nDistance in metres = %f ",m);
printf("\nDistance in feet = %f ",feet);
printf("\nDistance in inches = %f ",inch);
printf("\nDistance in centimetres = %f ",cm);
getch();
}
|
Output:
Enter the distance between two cities(in km) – 20
Distance in kilometres = 20.000000
Distance in metres = 20000.000000
Distance in feet = 65616.796875
Distance in inches = 787402.000000
Distance in centimetres = 2000000.000000
Free online measurement converter tool Visit us at: http://www.onlineconversions.org/
ReplyDeleteRead next: C Program to convert distance in meters, feet, inches, and centimeters
ReplyDeleteIn math 1km= 1000m
ReplyDelete1m=1km/1000,but in c code the solution is inverted
Why ?
ReplyDelete