FORTRAN Programming Assignment Guide
This document contains FORTRAN code examples for the assignment.
A. Input and Output
! Simple Input and Output Example
PROGRAM IOExample
IMPLICIT NONE
INTEGER :: age
PRINT *, 'Enter your age:'
READ *, age
PRINT *, 'Your age is:', age
END PROGRAM IOExample
B. Loops
! Loop Example (Printing numbers from 1 to 10)
PROGRAM LoopExample
IMPLICIT NONE
INTEGER :: i
DO i = 1, 10
PRINT *, 'Number:', i
END DO
END PROGRAM LoopExample
C. Decisions
! Decision Making Example (If-Else Statement)
PROGRAM DecisionExample
IMPLICIT NONE
INTEGER :: number
PRINT *, 'Enter a number:'
READ *, number
IF (number > 0) THEN
PRINT *, 'The number is positive.'
ELSE IF (number < 0) THEN
PRINT *, 'The number is negative.'
ELSE
PRINT *, 'The number is zero.'
END IF
END PROGRAM DecisionExample
D. Arrays
! Array Example (Storing and Printing Values)
PROGRAM ArrayExample
IMPLICIT NONE
INTEGER, DIMENSION(5) :: numbers
INTEGER :: i
PRINT *, 'Enter 5 numbers:'
DO i = 1, 5
READ *, numbers(i)
END DO
PRINT *, 'The numbers you entered are:'
PRINT *, numbers
END PROGRAM ArrayExample
E. Arithmetic/Assignment Statements
! Arithmetic Operations Example
PROGRAM ArithmeticExample
IMPLICIT NONE
REAL :: a, b, sum, difference, product, quotient
PRINT *, 'Enter two numbers:'
READ *, a, b
sum = a + b
difference = a - b
product = a * b
quotient = a / b
PRINT *, 'Sum:', sum
PRINT *, 'Difference:', difference
PRINT *, 'Product:', product
PRINT *, 'Quotient:', quotient
END PROGRAM ArithmeticExample
F. Subroutines
! Subroutine Example (Calculating Square)
PROGRAM SubroutineExample
IMPLICIT NONE
REAL :: number, result
PRINT *, 'Enter a number:'
READ *, number
CALL Square(number, result)
PRINT *, 'The square of the number is:', result
CONTAINS
SUBROUTINE Square(x, y)
REAL, INTENT(IN) :: x
REAL, INTENT(OUT) :: y
y = x * x
END SUBROUTINE Square
END PROGRAM SubroutineExample