C     Programmer:  Stephen Anderson
C     Course:  CS430, Section 0001
C     Date Due:  September 13, 2007
C     Language:  FORTRAN 77
C     Compiler used:  Force 2.0
C     Source Filename:  Project2A.f
C     Executable Filename (if any) or None:  None
C     Professor:  Dr. Adams

C     Description of the program's PURPOSE:
C      Reads in an integer in BASE 10 and prints representation of the
C      integer in BASE 2 (BINARY)

C     Description of program's required INPUT:
C      The input must be an integer.
C      The integer value has to be between 1 and 32767.
C      The input must only contain digits, no commas or decimals.

C     Description of program's expected OUTPUT:
C      The output will print input value in BASE 10 followed by
C      the integer's value in BASE 2, which is binary.

C     Descriptions of any MODIFICATIONS made in debugging the program:
C      The output was originally going to be a 15-digit-long integer
C      value, but FORTRAN did not allow a number that long to be an
C      integer.  Now each digit of the BASE 2 representation is stored
C      in an array that has a size of 15.

C     General COMMENTS about the PROGRAM:
C      The input has to be an integer and between 1 and 32767.
C      After the program reads in the input value, a 0 is stored in each
C      index of the array.
C      A DO-LOOP compares the value to powers of 2.
C      The first run in the loop checks if the value is equal or greater
C      than 2 raised to the 14th power (2^14).  If the value is equal or
C      greater than 2^14, then the 0 in BINARY(15) is replaced with 1.
C      Then the 2^14 is subtracted from the input value.  If the value
C      is less than 2^14, the DO-LOOP loops again to compare the value
C      to 2^13.  The process is repeated until 2^0, which is represented
C      by BINARY(1).
C      The array is then printed off from index 15 to index 1, giving
C      the BASE 2 representation of the index value.

C     General COMMENTS explaining the syntax or features of the LANGUAGE
C      The array that holds the BASE 2 (binary) representation is
C      an integer array, is called BINARY, and has a size of 15.
C      The integer value NUM is the input integer in base 10.

C      The WRITE statements have 2 numbers in parenthesizes.  The first
C      number is the default output device and the second number is the
C      number associated with the FORMAT statement for the WRITE.
C      The FORMAT statements associated with the WRITE statements have
C      different statements in the parenthesizes.  A statement like A44
C      means character format of length 44.  A statement I5 means an
C      integer 5 digits long.  1X is one space over.

C      The READ statement has 2 numbers.  The 5 is the default input
C      device and the second number is the number associated with the
C      FORMAT statement.

C      In the DO-LOOPS, the value I is the counter for each loop and is
C      also used as the index of the BINARY array.

C      In the second DO-LOOP, the first IF statement checks to see
C      if a certain power of 2 is less than or equal to the input value.
C      The first time through, it checks 2^14.  The last time, it
C      checks 2^0.


C      Declaration of the array and the input value.
       INTEGER BINARY(15)
       INTEGER NUM
       
C      Prompts and reads the input value and stores it in NUM
       WRITE (6,21) 'Please enter an integer between 1 and 32767.'
  21   FORMAT (A44)
       READ (5,22) NUM
  22   FORMAT (I5)

C      Prints out the input value
       WRITE (6,23) 'The base 10 value of the input is: ', NUM
  23   FORMAT (1X/(A35, I5)/1X)

C      Assigns each index of the array as 0
       DO 24 I = 1, 15, 1
          BINARY(I) = 0
  24   CONTINUE

C      Loops to find where in the array the 1s are.
       DO 25 I = 15,1,-1
          IF ((2**(I-1) .LT. NUM) .OR. (2**(I-1) .EQ. NUM))
     C       BINARY(I) = 1
          IF (BINARY(I) .EQ. 1)
     C       NUM = NUM - 2**(I-1)
  25   CONTINUE

C      Prints out the BASE 2 representation of the input value
       WRITE (6,51) 'The base 2 value of the input is: ',
     C (BINARY(I), I = 15,1,-1)
  51   FORMAT (A34, 15(I1))
   
       STOP
       END
