PROGRAM Binary_Converter
!-----------------------------------------------------------------------------
! Program to find the binary representation of a positive integer.
! Variables used are:
!   Number    : positive integer to be converted
!   Remainder : one of the remainders in the repeated-division method
!   RemList   : pointer to first node in the linked list of remainders
!   TempPtr   : pointer used in constructing the list
!   CurrPtr   : pointer used in traversing the list
!   Data      : node component that stores an integer
!   Next      : node component that points to the next node
!
! Input:   A positive integer
! Output:  The binary representation of this integer
!-----------------------------------------------------------------------------

  TYPE List_Node
    INTEGER :: Data
    TYPE (List_Node), POINTER :: Next
  END TYPE List_Node

  TYPE (List_Node), POINTER :: RemList, TempPtr, CurrPtr

  INTEGER :: Number, Remainder

! Get a positive integer to be converted
  DO
    WRITE(*, '(1X, A)', ADVANCE = "NO") "Enter positive integer: "
    READ *, Number
    IF (Number > 0) EXIT
  END DO

  Write(*, 10, ADVANCE = "NO") Number
  10 FORMAT(" Binary representation of", I6, " is:  ")

! Initialize RemList to be an empty linkedlist
   NULLIFY(RemList)

! While Number > 0,  divide Number by 2 and  
! add remainder at beginning of RemList
  DO
    IF (Number == 0) EXIT
    Remainder = MOD(Number, 2)
    ALLOCATE(TempPtr)
    TempPtr%Data = Remainder
    TempPtr%Next => RemList
    RemList => TempPtr
    Number = Number / 2
  END DO

! Traverse the list and display the binary digits
  CurrPtr => RemList
  DO
    IF (.NOT. ASSOCIATED(CurrPtr)) EXIT
    Write(*, '(I1)', ADVANCE = "NO") CurrPtr%Data
    CurrPtr => CurrPtr%Next
  END DO
  PRINT *

END PROGRAM Binary_Converter
