C     Programmer: Chris Golding
C     Course: CS 430
C     Date Due: 9/13/07
C     Languate: Fortran
C     Compiler Used: Force 2.0
C     Executable File Name: PA2-A.exe
C     Professor: Adams

C     Description of the program's PURPOSE:
C     To read user inputed base 10 values between 1 and 32767 and convert
C     it to base 2 and then output it to the user.

C     Description of the program's required INPUT:
C     The user will input an integer between 1 and 32767.

C     Descriptoin of the program's expected OUTPUT:
C     The program will echo the user's inputed base 10 value and then
C     output the value in base 2.

C     Descrptions of any MODIFICATIONS made in debugging the program:
C     The most difficult part of this program was getting the format
C     statments to work properly with my output.  I used Arrays to hold
C     the converted base 2 values, but when outputing them each cell in
C     the array would have its own line, I fixed this by using the format
C     statement 15F1, which outputs 15 values and each value is 1 digit
C     onto a single line.

C     Gernal COMMENTS about the PROGRAM:
C     No error checking is performed
C     The program exits on successful run.

C     General COMMENTS explaining the syntax or features of the LANGUAGE
C     This program required me to examine, the use of using IF statements
C     without the use of the ENDIF.

      DIMENSION N(15)              !Array for holding base 2 value
      
      MAXLENGTH = 15               !Max number of digits base 2 number could be

      WRITE (6, 10)                !Prompt user for input
      READ (5, 21) INPUT           !Reads user input
      IF (INPUT.LT.32767.AND.INPUT.GT.1) GO TO 4 !Checks for valid value
      WRITE (6, 25)
      GO TO 5
      
 4    WRITE (6, 24) INPUT          !Echo user inputed value

C     Converts base 10 number to base 2 using mod conversion
      DO 30 I=1, MAXLENGTH
         IF (INPUT.LT.1.OR.MOD(INPUT,2).EQ.0) N(I) = 0
         IF (MOD(INPUT,2).EQ.1) N(I) = 1
         INPUT = INPUT/2
 30   CONTINUE

      ISTART = 15
C     Removes excess 0s from output statement
      DO 31 I=1, 15
         IF (N(I).EQ.1) ISTART = I
 31   CONTINUE

      WRITE (6, 23) (N(I), I=ISTART,1,-1)    !Outputs base 2 value


C     Formatting satements for input and output.
 10   FORMAT (1X, "WELCOME PLEASE ENTER A NUMBER BETWEEN 1 AND 32,767")
 21   FORMAT (I5)
 23   FORMAT (1X, "THE BASE 2 VALUE OF THE INPUT IS: ", 15I1)
 24   FORMAT (1X, "THE BASE 10 VALUE OF THE INPUT IS: ",  I5)
 25   FORMAT (/1X, "THE VALUE ENTERED IS NOT A VALID VALUE.")
 
 5    STOP
      END
