Matrix Multiplication | FORTRAN 95

Matrix Multiplication | FORTRAN 95

PROGRAM matrix_multiply
  IMPLICIT NONE
  INTEGER::i,j,m,n,p,q,k
  REAL::s
  REAL, DIMENSION(10,10)::mat1,mat2,res

  PRINT *, '============================================'
  PRINT *, 'PROGRAM TO MULTIPLY 2 MATRICES [ BY WWW.BOTTOMSCIENCE.COM ]'
  PRINT *, '============================================'

  PRINT *, 'FOR MATRIX 1'
  PRINT *, 'ENTER THE NUMBER OF ROWS'
  READ(*,*) m
  PRINT *, 'ENTER THE NUMBER OF COLUMNS'
  READ(*,*) n
  PRINT *, 'ENTER THE ELEMENTS'

    READ(*,*) ((mat1(i,j),j=1,n),i=1,m)

  PRINT *, 'FOR MATRIX 2'
  PRINT *, 'ENTER THE NUMBER OF ROWS'
  READ(*,*) p
  PRINT *, 'ENTER THE NUMBER OF COLUMNS'
  READ(*,*) q
  PRINT *, 'ENTER THE ELEMENTS'

    READ(*,*) ((mat2(i,j),j=1,q),i=1,p)

  IF(n==p) THEN
    DO i=1,m
      DO j=1,q
        s=0
        DO k=1,n
          S=s+mat1(i,k)*mat2(k,j)
        END DO
        res(i,j)=s
      END DO
    END DO
    
    DO i=1,m
      WRITE(*,*) (res(i,j),j=1,q)
    END DO

  ELSE
    PRINT *,'MULTIPLICATION NOT POSSIBLE (DIMENSION ERROR)'
  END IF
END PROGRAM

OUTPUT

OUTPUT - MATRIX MULTIPLICATION
OUTPUT – MATRIX MULTIPLICATION

Leave a Reply

Your email address will not be published.