C program to find the distance between two cities in different units

C program to find the distance between two cities in different units:

In this post, we will learn how to find the distance between two cities in C. The distance is given in kilometers and the program will convert this value to inches, centimeters, feet and meters.

It will take the kilometer distance as input from the user, convert this value to other units and print the converted values to the user. With this program, you will learn how to take user inputs in C, how to do basic mathematical calculations and how to print results on the console.

Algorithm:

Before we start to write the program, we have to learn how to do the conversion between different units.

Following are the conversions of 1km to inch, centimeter, feet and meter:

1 km = 39370.1 inch
1 km = 100000 cm
1 km = 3280.84 feet
1 km = 1000 meter

So, if we get any kilometer value, we can multiply this value by these values to convert it to the respective unit values.

Example 1: C program to convert a kilometer value to inches, centimeter, feet and meter:

Below is the complete C program:

#include <stdio.h>

int main()
{
	float km;
	float inch, cm, feet, m;

	printf("Enter the distance between the cities in km: ");
	scanf("%f", &km);

	inch = km * 39370.1;
	cm = km * 100000;
	feet = km * 3280.84;
	m = km * 1000;

	printf("%.2f km = %.2f inches\n", km, inch);
	printf("%.2f km = %.2f cms\n", km, cm);
	printf("%.2f km = %.2f feet\n", km, feet);
	printf("%.2f km = %.2f m\n", km, m);
	return 0;
}

Here,

  • The kilometer value is assigned to the km variable.
  • This value is converted to inch, centimeter, feet and meter.
  • The printf lines are printing the converted values.

It will print output as below:

Enter the distance between the cities in km: 10
10.00 km = 393701.00 inches
10.00 km = 1000000.00 cms
10.00 km = 32808.40 feet
10.00 km = 10000.00 m

Example 2: C program to convert a kilometer value to inches, centimeter, feet and meter using macros:

Macros can be used to make the code readable. You can define the constants with macros before the program starts. If you are working on a large project with multiple files, you can create one utility file for all constants and utility methods.

Let’s use different macros to define the unit conversion constants:

#include <stdio.h>
#define KM_TO_INCH 39370.1
#define KM_TO_CM 100000
#define KM_TO_FEET 3280.84
#define KM_TO_METER 1000

int main()
{
	float km;
	float inch, cm, feet, m;

	printf("Enter the distance between the cities in km: ");
	scanf("%f", &km);

	inch = km * KM_TO_INCH;
	cm = km * KM_TO_CM;
	feet = km * KM_TO_FEET;
	m = km * KM_TO_METER;

	printf("%.2f km = %.2f inches\n", km, inch);
	printf("%.2f km = %.2f cms\n", km, cm);
	printf("%.2f km = %.2f feet\n", km, feet);
	printf("%.2f km = %.2f m\n", km, m);
	return 0;
}

We defined four macros. Each holds one constant value, conversion of one kilometer to inch, cm, feet and meter. So, instead of using these values directly, we can use the macros.

It will give similar results.

Enter the distance between the cities in km: 23
23.00 km = 905512.31 inches
23.00 km = 2300000.00 cms
23.00 km = 75459.32 feet
23.00 km = 23000.00 m

You might also like: