C      Programmer: Greg Marut
C      Course: CS 430
C      Date Due: September 12, 2007
C      Language: FORTRAN
C      Compiler used: FORCE 2.08
C      Source Filename: HW2.f
C      Executable Filename: HW2.EXE
C      Professor: Elizabeth Adams

C      Description of the program's PURPOSE:
C      The purpose of this program is to take input and convert it to
C      base 2. The first input is an integer and the second is a
C      decimal.

C      Description of program's required INPUT:
C      The first input is an integer between 1 and 32767 and the second
C      input is a decimal of up to 6 places long.

C      Description of program's expected OUTPUT:
C      This program will first display a prompt asking the user to
C      enter an integer and then display both the original integer
C      entered and its base 2 equivalant. It will then prompt the user
C      for a decimal and print out both the decimal and its base 2
C      equivalant.

C      Descriptions of any MODIFICATIONS made in debugging the program:
C      None

C      General COMMENTS about the PROGRAM
C      The program will start by declaring 2 dimensions. The first one
C      is used to store the 15 bits of of the first integer entered and
C      the second dimension is used to store the 6 bits of the decimal
C      that is entered. The program then prompts the user to enter in
C      a number between 1 and 32767. Once the user enters that integer,
C      it is stored in the variable "IN". A second variable is then
C      created called "INNEW". This variable will be used to keep track
C      of the integer as we calcualte the binary number. A do loop is
C      then called to actually calculate the binary number of the
C      integer. After the binary number is generated, both the original
C      integer and the binary number are printed to screen. Next, a
C      prompt is given to the user to enter in a decimal number. This
C      decimal is stored in a variable called "DECIMALIN" and then a
C      do loop is called to calculate the binary number. Then the
C      program prints out the original decimal number and the binary
C      equivalant.

C      General COMMENTS explaining the syntax or features of the LANGUAGE
C      Implied do loops are called to print out the binary numbers
C      since they are stored in dimensions.

C       *** START OF PROGRAM ***
C       Create a dimension of size 15 and of size 6. This is to hold a
C       1 or a 0 for the corresponding binary number.
        DIMENSION IBINARY(15)
        DIMENSION IBINARYB(6)

C       PART A
C       Print out a prompt and read input from the user.
        WRITE (6, 10)
        READ (5, 15) IN

  10    FORMAT (1X, 'Please enter an integer between 1 and 32767 ', 1X)
  15    FORMAT (I5)

C       Create a new variable INNEW and assign it to the input "IN"
        INNEW = IN

C       Loop backwards from 14 to 0 and calculate 2^n to find out if
C       the number is divisible by 2^n. If so, subtract 2^n from the
C       number and put a 1 for that slot in the binary number. If the
C       number is less than 2^n, put a 0 in the slot for that binary
C       number.
        DO 30 N = 14, 0, -1
           IF(2**N .LE. INNEW) GO TO 31
           IBINARY(N + 1) = 0
           GO TO 30
  31       INNEW = INNEW - (2**N)
           IBINARY(N + 1) = 1
  30    CONTINUE

C       Print out the orginal output as well as the binary equivalent.
        WRITE (6, 20) IN
        WRITE (6, 21) (IBINARY(I), I = 15, 1, -1)
  
  20    FORMAT (1X, 'The base 10 value of the input is: ', I5)
  21    FORMAT (1X, 'The base 2 value of the input is: ', 15I1)
  
C       PART B
C       Print out a prompt and read input from the user.
        WRITE (6, 40)
        READ (5, 45) DECIMALIN

  40    FORMAT (/, /, 1X, 'Please enter a decimal number ', 1X)
  45    FORMAT (F7.6)

C       Create a new variable DECIMALNEW and assign it to the input
C       "DECIMALIN"
        DECIMALNEW = DECIMALIN

C       Loop from 1 to 6 and multiply DECIMALNEW by 2. Then check to
C       see if DECIMAL NEW is greater than or equla to 1. If so,
C       subtract 1 from DECIMAL NEW and put a 1 in that spot for the
C       binary number. If it is less than 1, put a 0 in that spot
C       for the binary number.
        DO 50 L = 1, 6, 1
           DECIMALNEW = DECIMALNEW * 2
           IF(DECIMALNEW .GE. 1) GO TO 51
           IBINARYB(L) = 0
           GO TO 50
  51       DECIMALNEW = DECIMALNEW - 1
           IBINARYB(L) = 1
  50    CONTINUE
  
C       Print out the orginal output as well as the binary equivalent.
        WRITE (6, 60) DECIMALIN
        WRITE (6, 61) (IBINARYB(I), I = 1, 6, 1)

  60    FORMAT (1X, 'The base 10 value of the input is: ', F7.6)
  61    FORMAT (1X, 'The base 2 value of the input is: .', 6I1)
        STOP
        END
C       *** END OF PROGRAM ***
