answersLogoWhite

0


Best Answer

This is not a perfect program, but it will get you started in the right direction. Works for any INTEGER up to "some" power of 2 (decimals kill the program).

PROGRAM binary

IMPLICIT NONE

INTEGER remainder, quotient, n, int_input, answer

REAL input, dec_input

WRITE(*,*) 'Input a number to convert to binary'

READ(*,*) input

int_input = input

dec_input = input - int_input

dec_input = abs(dec_input)

quotient = abs(input)

DO WHILE (dec_input==0)

n = 0

answer = 0

DO WHILE (quotient>1)

remainder = mod(quotient,2)

quotient = quotient/2

answer = answer+remainder*10.**n

n = n+1

END DO

IF (input<0) answer = -answer

answer = answer + quotient*10.**n

WRITE(*,"(a,i31)") 'Your answer in binary is:',answer

END DO

END PROGRAM binary

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Conversion of decimal to binary in fortran?
Write your answer...
Submit
Still have questions?
magnify glass
imp