Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Monday, July 13, 2020

C program to convert kilometer to cm, meter, feet and mile

C Program to convert kilometer to meter, cm, feet & mile:
By Ahmad Irshad | Programming Exercises and Solutions > Program to convert km to meter, cm, ft & 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

Distance convertor | Protec

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.

  1. INPUT

  2. 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: 

  1. /* C program to to convert kilometer to meter, cm, feet & mile */
  2. #include <stdio.h>

  3. int main() { float km, meter, cm, feet, mile; /* Input distance in kilometers from user */ printf("Enter distance [in Kilometers]: "); scanf("%f", &km); /* Convert km to meter, cm, feet and miles */ meter = km * 1000.0; // since, 1km = 1000 m
  4. cm = km * 1000.0 * 100.0 // since, 1 km = 100000 cm
  5. feet = km * 3280.84 // since, 1 km = 3280.84 feets mile = km * 0.6214 // since, 1 km = 0.621371 miles
  6. /* Print the value of meter, cm, feet, & mile */ printf("%.2f Kilometers = %.2f m", km, meter);
  7. printf("%.2f Kilometers = %.2f cm", km, cm);
  8. printf("%.2f Kilometers = %.2f feets", km, feet);
  9. printf("%.2f Kilometers = %.2f miles", km, mile);
  10. 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:

  1. Enter distance [in Kilometers] = 1
  2. 1 kilometers = 1000.00 m
  3. 1 kilometers = 100000.00 cm
  4. 1 kilometers = 3280.84 feets
  5. 1 kilometers = 0.62 miles
















Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

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.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us