Given as input a floating (real) number of centimeters, print out the equivalent number of feet (integer) and inches (floating, 1 decimal), with the inches given to an accuracy of one decimal place.

    Assume 2.54 centimeters per inch, and 12 inches per foot.
    If the input value is 333.3, the output format should be:
    333.3 centimeters is 10 feet 11.2 inches.

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>

int main(int argc, char ** argv) {
    float feet;
    float inch, cm;
    printf("Enter number (cm): ");
    scanf("%f", &cm);

    inch = cm / 2.54;
    feet = inch / 12;

    printf(" %.1f centimeters is %d feet %.1f inches\n", cm, (int)feet, inch);
    return 0;

When you compile and execute the above program it produces the following result on Linux:

Enter number (cm): 40
40.0 centimeters is 1 feet 15.7 inches

Brief Explanation

  • The program requests the user to enter a number in centimeters (cm).
  • scanf function is used to read the number and assign it to the variable cm.
  • To compute inch, cm is divided by 2.54
  • To compute feet, the inch obtained above is divided by 12
  • printf function is used to display the results. Since the exercise required us to display the number of feet as integer, casting is used in the printf function “(int)feet” to coerce the result to our desired type.

Add comment

Security code