In this c program, we will convert distance input by the user into meters, feet, inches, and centimeters. We will also write the algorithm of the c program to convert distance into meters, feet, inches, and centimeters. We will firstly break down the problem and write an algorithm, then after that, we will write the c program and obtain the required output.
We need to take the distance (in kilometers) from the user in kilometers and then convert it into meters, feet, inches, and centimeters and display the output on the screen.
This tutorial is broken down in following parts:
- Concepts
- Algorithm
- Program
- Output
- Resources
Let’s write this c program to convert distance into meters, feet, inches, and centimeters. But before that, let’s understand all the concepts that we are gonna use in our program.
1. Concepts
So, we have the distance in kilometers that is input by the user and we need to display it in meters, feet, inches, and centimeters through a c program, and display them separately on the output screen.
Formula to calculate the distance into different units:
Meter = km * 1000 Feet = km * 3280.84 Inches = km * 39370.1 Centimeter = km * 100000
What are the concept of c programming you need to learn before writing this program?
- scanf() function
- Operators
- printf() function
Kindly, learn these basic topics, before continuing and writing this C Program.
2. Algorithm
Here’s the algorithm for writing this C Program to convert distance into meters, feet, inches, and centimeters:
- START
- INPUT DISTANCE
- Meter = km * 1000
- Feet = km * 3280.84
- Inches = km * 39370.1
- Centimeter = km * 100000
- DISPLAY Meter
- DISPLAY Feet
- DISPLAY Inches
- DISPLAY Centimeter
- STOP
Follow the same algorithm and let’s write the actual c program.
3. C Program to convert distance into meters, feet, inches, and centimeters
Here’s the C Program to convert distance into meters, feet, inches, and centimeters:
/* C Program to convert distance in meter, feet, inches, centimeter */
#include <stdio.h>
#include <conio.h>
int main() {
int distance;
float meter, feet, inches, centimeter;
printf("Enter the distance [in Kilometers]: ");
scanf("%d", & distance);
meter = distance * 1000;
feet = distance * 3280.84;
inches = distance * 39370.1;
centimeter = distance * 100000;
printf("Meter = %f\n", meter);
printf("Feet = %f\n", feet);
printf("Inches = %f\n", inches);
printf("Centimeters = %f\n", centimeter);
getch();
}
4. Output
As we have written out C Program, let’s have a look at the out. We are giving the input distance as 520 kilometers and now, here are the outputs.
