Cm and Inch Conversion

Problem Statement

It is known that 1 cm is equal to 0.3937 inch and 1 inch is equal to 2.54 cm. Write a program to convert 0, 0.5, 1, 1.5, ..., 8, 8.5, 9, 9.5, and 10 from cm to inch and from inch to cm.

Solution

! ---------------------------------------------------------------
!    This program "contains" two REAL functions:
!         (1)  Cm_to_Inch() takes a real inch unit and converts
!              it to cm unit, and
!         (2)  Inch_to_cm() takes a real cm unit and converts it
!              to inch unit.
! The main program uses these functions to convert 0, 0.5, 1, 1.5,
! 2.0, 2.5, ..., 8.0, 8.5, 9.0, 9.5 and 10.0 inch (resp., cm) to
! cm (resp., inch).
! ---------------------------------------------------------------

PROGRAM  Conversion
   IMPLICIT  NONE

   REAL, PARAMETER :: Initial = 0.0, Final = 10.0, Step = 0.5
   REAL            :: x

   x = Initial
   DO                         ! x = 0, 0.5, 1.0, ..., 9.0, 9.5, 10
      IF (x > Final)  EXIT
      WRITE(*,*)  x, 'cm = ',   Cm_to_Inch(x), 'inch and ',  &
                  x, 'inch = ', Inch_to_Cm(x), 'cm'
      x = x + Step
   END DO

CONTAINS

! ---------------------------------------------------------------
! REAL FUNCTION  Cm_to_Inch()
!    This function converts its real input in cm to inch.
! ---------------------------------------------------------------

   REAL FUNCTION  Cm_to_Inch(cm)
      IMPLICIT  NONE

      REAL, INTENT(IN) :: cm
      REAL, PARAMETER  :: To_Inch = 0.3937   ! conversion factor

      Cm_to_Inch = To_Inch * cm
   END FUNCTION  Cm_to_Inch

! ---------------------------------------------------------------
! REAL FUNCTION  Inch_to_Cm()
!    This function converts its real input in inch to cm.
! ---------------------------------------------------------------

   REAL FUNCTION  Inch_to_Cm(inch)
      IMPLICIT  NONE

      REAL, INTENT(IN) :: inch
      REAL, PARAMETER  :: To_Cm = 2.54       ! conversion factor

      Inch_to_Cm = To_Cm * inch
   END FUNCTION  Inch_to_Cm

END PROGRAM  Conversion
Click here to download this program.

Program Input and Output

The following is the output from the above program.
 0.E+0cm = 0.E+0inch and 0.E+0inch = 0.E+0cm
 0.5cm = 0.196850002inch and 0.5inch = 1.26999998cm
 1.cm = 0.393700004inch and 1.inch = 2.53999996cm
 1.5cm = 0.590550005inch and 1.5inch = 3.80999994cm
 2.cm = 0.787400007inch and 2.inch = 5.07999992cm
 2.5cm = 0.984250009inch and 2.5inch = 6.3499999cm
 3.cm = 1.18110001inch and 3.inch = 7.61999989cm
 3.5cm = 1.37794995inch and 3.5inch = 8.88999939cm
 4.cm = 1.57480001inch and 4.inch = 10.1599998cm
 4.5cm = 1.77165008inch and 4.5inch = 11.4300003cm
 5.cm = 1.96850002inch and 5.inch = 12.6999998cm
 5.5cm = 2.16534996inch and 5.5inch = 13.9699993cm
 6.cm = 2.36220002inch and 6.inch = 15.2399998cm
 6.5cm = 2.55905008inch and 6.5inch = 16.5100002cm
 7.cm = 2.75589991inch and 7.inch = 17.7799988cm
 7.5cm = 2.95274997inch and 7.5inch = 19.0499992cm
 8.cm = 3.14960003inch and 8.inch = 20.3199997cm
 8.5cm = 3.34645009inch and 8.5inch = 21.5900002cm
 9.cm = 3.54330015inch and 9.inch = 22.8600006cm
 9.5cm = 3.74014997inch and 9.5inch = 24.1299992cm
 10.cm = 3.93700004inch and 10.inch = 25.3999996cm

Discussion