I have a problem in my text that wants me to write a basic Fortran 95/2003 progr
ID: 3638704 • Letter: I
Question
I have a problem in my text that wants me to write a basic Fortran 95/2003 program that will ask the user for a number within a specific range, and then convert it to its binary equivalent. I wrote a program, but it just won't work! It keeps telling me the binary number is 0000000000 no matter what I do. (I'm a noob please help lol)Actual Question asks:
Write a program that prompts the user for a decimal integer within the range of 0 to 1023, and converts the number into its binary equivalent. The binary number should consist of 0's and 1's in a character string, and should display the binary number to the user.
My code:
!Purpose: Converts decimal integer in range 0 to 1023 to binary.
!Author: Anthony Pecchillo
!Date: 2/10/2012
Program DectoBin
implicit none !Turn of implicit typing
integer :: dec !Input Var - Integer from 0 to 1023.
integer :: i !Internal Var - Counter for the loop.
integer :: L !Internal Var - Exponent in loop.
integer :: bin(10) !Output Var - Controls which exponent I'm checking.
write(*,*) "Enter a decimal 0 to 1023 :" !Prompt the user for dec
read(*,*) dec !Read in dec
if((dec < 0).or.(dec > 1023)) STOP "Out of range. Program end."
if((dec >= 0).and.(dec <= 1023)) then
do i = 9,0
L = i
if (dec > 2**L) then
bin(10-L) = 1
dec = dec-2**L
else
bin(10-L) = 0
endif
enddo
endif
write(*,*) "Your number in binary is:",(bin(i),i=1,10)
stop
end program DectoBin
PLEASE PLEASE PLEASE HELP ME!
Full Karma to anyone who can fix it and makes it work in my terminal after I compile it. IT MUST BE IN FORTRAN NO OTHER LANGUAGES!!!!
Explanation / Answer
Check is it compiling, i might have some mistakes in printing the array, but the logic is correct. Program DectoBin implicit none !Turn of implicit typing integer :: dec !Input Var - Integer from 0 to 1023. integer :: i !Internal Var - Counter for the loop. integer :: t !counter for the output integer, dimension(10) :: binary !here you will keep the 0's and 1's write(*,*) "Enter a decimal 0 to 1023 :" !Prompt the user for dec read(*,*) dec !Read in dec if((dec < 0).or.(dec > 1023)) STOP "Out of range. Program end." if((dec >= 0).and.(decRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.