PROGRAM Angles_2
!----------------------------------------------------------------------
! Program demonstrating the use of an internal subroutine 
! Print_Degrees_Minutes_Seconds to display an angle measured in
! radians or degrees in degrees-minutes-seconds format.
! Variables used are:
!     Angle    : angle measurement
!     Measure  : "R" if radian measure, "D" if degree measure
!     Response : user response to more-data question
!
! Input:  Angle, Radian, and Response
! Output: Value in Angle in degrees-minutes-seconds format (displayed
!         by Print_Degrees_Minutes_Seconds )
!----------------------------------------------------------------------

  REAL :: Angle
  CHARACTER(1) :: Measure, Response

  ! Read and convert angles until user signals no more data
  DO
     WRITE (*, '(1X, A)', ADVANCE = "NO") &
           "Enter angle and R if in radians, D if in degrees: "
     READ *, Angle, Measure
     CALL Print_Degrees_Minutes_Seconds(Angle, Measure)
     WRITE (*, '(/ 1X, A)', ADVANCE = "NO") "More angles (Y or N)? "
     READ *, Response
     IF (Response /= "Y") EXIT
  END DO

CONTAINS

  !-Print_Degrees_Minutes_Seconds--------------------------------------
  ! Subroutine to display an angular measurement Angle in either
  ! radians or degrees in a degrees-minutes-seconds format.
  ! Measure is "R" or "D" according to whether Angle is given 
  ! in radians or degrees.  Local identifiers used are:
  !   Pi               : the constant pi
  !   Angle_in_Degrees : the degree equivalent of Angle
  !   Degrees          : the number of degrees
  !   Minutes          : the number of minutes
  !   Seconds          : the number of seconds
  !
  ! Accepts:  Angle and Radian
  ! Output:   Value of Angle in degrees-minutes-seconds format
  !--------------------------------------------------------------------

    SUBROUTINE Print_Degrees_Minutes_Seconds(Angle, Measure)
  
    REAL, INTENT(IN) :: Angle
    CHARACTER(*), INTENT(IN) :: Measure
    REAL :: Angle_in_Degrees
    REAL, PARAMETER :: Pi = 3.141593
    INTEGER :: Degrees, Minutes, Seconds
  
    ! First get the degree equivalent of the angle
  
    IF (Measure == "R") THEN
       PRINT 10, Angle, "radians"
       Angle_in_Degrees = (180.0 / Pi) * Angle
    ELSE IF (Measure == "D") THEN
       PRINT 10, Angle, "degrees"
       Angle_in_Degrees = Angle
    ELSE
       PRINT *, Measure, " is an illegal type of measure"
       RETURN
    END IF
    10 FORMAT (1X, F10.5, 1X, A / 1X, " is equivalent to")
  
    ! Now determine the number of degrees, minutes, and seconds
  
    Degrees = INT(Angle_in_Degrees)
    Angle_in_Degrees = Angle_in_Degrees - REAL(Degrees)
    Degrees = MOD(Degrees, 360)
    Angle_in_Degrees = 60.0 * Angle_in_Degrees
    Minutes = INT(Angle_in_Degrees)
    Angle_in_Degrees = Angle_in_Degrees - REAL(Minutes)
    Seconds = INT(Angle_in_Degrees * 60.0)
  
    PRINT 20, Degrees, Minutes, Seconds
    20 FORMAT (1X, I4, " degrees,", I3, " minutes,", I3, " seconds")
  
  END SUBROUTINE Print_Degrees_Minutes_Seconds
  
END PROGRAM Angles_2
