Read a height in centimeters and displays the height in centimeters and in feet and inches. - C Operator

C examples for Operator:Arithmetic Operator

Description

Read a height in centimeters and displays the height in centimeters and in feet and inches.

Demo Code

#include <stdio.h>

const float CM_PER_IN = 2.54;
const int IN_PER_FT = 12;

int main(void)
{
  float height_cm, height_in, inches;
  int feet;//from   w  w  w.j  a  v a2s  . c  o  m

  printf("Enter a height in centimeters: ");
  scanf("%f", &height_cm);

  while (height_cm > 0){
    height_in = height_cm / CM_PER_IN; // convert height to inches
    feet = (int) height_in / IN_PER_FT; // get number of feet in height
    inches = height_in - feet * IN_PER_FT; // get remaining inches

    printf("%.1f cm = %d feet, %.1f inches\n",
           height_cm, feet, inches);

    printf("Enter a height in centimeters (<= 0 to quit): ");
    scanf("%f", &height_cm);
  }
  return 0;
}

Result


Related Tutorials