program bintodec
! This program takes as input
! an 8 bit number bit by bit
! and gives the equivalent decimal
! number as output
! the bits are assumed to be entered MSB first
	integer :: n,m, dec
	
	dec = 0

	do n = 1, 8
	! bits are entered MSB first
	write (*,*) "Enter bit ",n
	read *, m
	! checks to see if anything other than
	! 1 or 0 has been entered
	if ((m.ne.0).and.(m.ne.1)) then
		write *, "Enter only 0 or 1"
		exit
	end if

	! entering one more bit means left shifting 
	! the existing binary number by 1 place, 
	! that in effect means multiplying the existing
	! number by 2. the recent most bit is added to this number
	! to get the current decimal equivalent
	! eq, if we have 110 (decimal equiv 6) and we enter 1
	! then the decimal number we get has value 2*6 + 1 = 13(binary 1101)

	dec = 2*dec + m
	end do

	print *, "the number is ", dec

	end program bintodec
