- 2071 Shrawan · 4 marks
Binary to decimal in FORTRAN
Short answer
Extract digits with MOD and integer division; accumulate decimal = Σ(digit × 2^position). Verify each digit is 0 or 1. For input 1011, output 11.
Write the program to convert a binary number to a decimal number using Fortran programming language.
Method. The binary number is read as an integer made of digits 0 and 1 (e.g. 1011). Repeatedly take the last digit with MOD(N,10), multiply by the power of 2 for its position, add it to the decimal value, and remove the digit with N/10 (integer division). A digit other than 0 or 1 is rejected.
PROGRAM BIN2DEC
INTEGER N, REM, DEC, P
PRINT *, 'ENTER A BINARY NUMBER'
READ *, N
DEC = 0
P = 0
10 IF (N .GT. 0) THEN
REM = MOD(N, 10)
IF (REM .GT. 1) THEN
PRINT *, 'NOT A BINARY NUMBER'
STOP
END IF
DEC = DEC + REM * 2**P
P = P + 1
N = N / 10
GO TO 10
END IF
PRINT *, 'DECIMAL VALUE = ', DEC
STOP
END
Sample: N = 1011 gives 1·8 + 0·4 + 1·2 + 1·1 = 11. N = 11010 gives 16 + 8 + 2 = 26.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Decimal to binary conversion · Computer Programming, chapter 5
- Structure of a FORTRAN program · Computer Programming, chapter 11 · asked 5×
- Define programming language · Computer Programming, chapter 1 · asked 3×
- Computer program and programming · Computer Programming, chapter 2
- Largest of n numbers in FORTRAN · Computer Programming, chapter 11
- Palindrome check in FORTRAN · Computer Programming, chapter 11
- Convert 37.432 decimal to binary · Digital Logic, chapter 1
- Convert octal 623.77 to decimal, binary, hex · Digital Logic, chapter 1
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Number conversions 33 to binary etc. · Basic Electronics Engineering, chapter 6