C      Programmer: Glenn Henderson
C      Course: CS 430
C      Date Due: September 12, 2007
C      Language: FORTRAN
C      Compiler used: FORCE2.08
C      Source Filename: PA2a.f
C      Executable Filename (if any) or None: None
C      Professor: Elizabeth Adams

C     Purpose: The purpose of this program is to convert a base 10
C              Number into a base 2 number
C
C     Input:   Any integer from 1 to 32767
C     Output:  The base 2 representation of the inputed integer
C     Language information
C        DIMENSION indicates that the variable name(s) which follow are arrays
C        The array is an integer array and its size is 15 elements
C        Print prints out a message, the * indicates default device
C        READ is the input statement, using a format of integer of size 5.
C        The If checks two conditions, that the number is less than the max
C            of 32767 and greater than the minimum of 1
C        If the number is out of bounds an appropriate statement is printed
C            and the program is terminated
C        If the number is in bounds we echo the number and then perform
C            the following, take the modulo of the number by 2
C            and that is the 1st digit in base 2, then divide the number
C            by 2 and store it back into the same variable
C            and continue modulo and dividing until all 15 spaces in the array are full
C        The NCOUNT variable is for printing the number of digits in the array
C            that are important
C        Once the base 2 string has been calculated it is then printed using
C            and implied do loop passing all the elements of the array to
C            the format statement
C        The format statement is accepting at most 15 integers, the maximum
C            numbers in the base 2 string for the maximum integer input



      DIMENSION I(15)
      INTEGER NUM
      INTEGER NCOUNT
      
      NCOUNT = 0

      PRINT *, 'Input a number between 1 and 32767 inclusively'

      READ (*, 10) NUM
   10 FORMAT (I5)

      IF (NUM .GT. 32767 .OR. NUM .LT. 1) THEN
         WRITE( *, 20) NUM
   20    FORMAT ('\nThe number ',I5,' is not in range')
      ELSE
         WRITE (*, 21) NUM
           
   21    FORMAT ('\nThe base 10 value of input is: ',I5)
            DO 30 J = 15 ,1,-1
               I(J) = MOD (NUM, 2)
               NUM = NUM / 2
               IF (NUM .NE. 0) NCOUNT = NCOUNT + 1
   30       CONTINUE
           
            WRITE (*,22) (I(K), K=(15-NCOUNT),15,1)
   22       FORMAT ('The base 2 value of the input is: ', 15I1)
      END IF

      PRINT *, '\nended successfully'
      STOP
      END
