Fortran    up
 
 


 Hello World   Fortran 77   Michael Neumann
*
C Hello World in Fortran 77
C (lines must be 6 characters indented)
*
      PROGRAM HELLOW
      WRITE(UNIT=*, FMT=*) 'Hello World'
      END
Prints "Hello World" onto the screen.


 Squares   Fortran 77   Michael Neumann
      PROGRAM SQUARE
      DO 15,I = 1,10
        WRITE(*, *) I*I
15    CONTINUE
      END
Outputs the squares from 1 to 10.


 Hello World   Fortran 90   Michael Neumann
PROGRAM HelloWorld
   PRINT *, "Hello World"
END PROGRAM HelloWorld
Prints "Hello World" onto the screen.


 Matrix Multiplication   Fortran 90   Michael Neumann
program TestMatrixMult

  real, dimension( 3, 3 ) :: A, B, C

  ! create identity matrix 
  A = 0.0; do i = 1, size(A); A(i,i) = 1.0; end do


  ! initialize matrix randomly
  call random_number( B )

  ! multiply matrices A and B
  C = matrixMult( A, B )

  ! output the three matrices
  print *, A, B, C

contains

!
! Alternatively use built-in function 'matmul' 
! (another built-in is 'dot_product') 
!
function matrixMult( A, B ) result ( C )

  implicit none

  real, dimension( :, : ), intent( in )     :: A  ! m x n
  real, dimension( :, : ), intent( in )     :: B  ! n x p
  real, dimension( size(A, 1), size(B, 2) ) :: C  ! m x p

  integer i, j

  do i = 1, size(C, 1)
    do j = 1, size (C, 2)
      C(i, j) = sum( A(i, :) * B(:, j) )
    end do
  end do

end function matrixMult

end program TestMatrixMult