Write a C program to Convert length in meters to feet and inches
Menu
Turn Mobile View Off
by Dinesh Thakur

Let us use variable meters of type float to represent length in meters and variables feet and inches of type int and float, respectively, to represent length in the FPS system. Let us also use variable tot_inches of type float to represent the given length in inches. To convert the length in meters to the FPS system, we first determine tot_inches, i. e., given length in inches as

           tot_inches = meters * 100.0 I CM_PER_INCH;

where CM_PER_INCH is a symbolic constant, with a value 2.54, used to represent the centimeters per inch. Then the length in feet and inches is calculated as follows:

feet = tot_inches I  12;

inches = tot_inches - feet * 12;

As the variable feet is of type int, only the integer part of the division tot_inches/12 is assigned to it. The program is given below. It first accepts the value of meters and calculates the values of feet and inches as explained above. Finally, the values of feet and inches are printed.


/* Convert length in meters to feet and inches */

#include <stdio.h>

#define CM_PER_INCH 2.54

int main ()

{

   float meters;    /* length in meters */

   int feet;        /* length in feet */

   float inches;    /* and inches */

   float tot inches; /* total inches */

   printf("Enter length in meters: ");

   scanf("%f", &meters);

   tot_inches =meters * 100.0 I CM_PER_INCH;

   feet = tot_inches I 12;

   inches = tot_inches - feet * 12;

   printf(''%.2fm = %d' %.2f\"\n", meters, feet, inches);

   return 0;

}

The program output is given below.

Enter length in meters: 1

1.00m = 3' 3.37"

Take a close look at the last printf statement in this program. Note the use of the escape sequence \" to print a double quote in the output. Also note that we have omitted the minimum field width in the conversion specifications for variables metersĀ· and inches as %. 2f. This causes the output values to be printed using minimum field width, i. e., closely spaced.

Dinesh Thakur is a Technology Columinist and founder of Computer Notes and Technology Motivation. Turn Mobile View Off