Matrix Addition | FORTRAN 95

Matrix Addition | FORTRAN 95

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

  PRINT *, '============================================'
  PRINT *, 'PROGRAM TO ADD 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(m==p .and. n==q) THEN
    DO i=1,m
      DO j=1,q
        res(i,j)=mat1(i,j)+mat2(i,j)
      END DO
    END DO
    DO i=1,m
      WRITE(*,*) (res(i,j),j=1,q)
    END DO
  ELSE
    PRINT *,'MATRIX ADDITION NOT POSSIBLE (DIMENSION ERROR)'
  END IF
END PROGRAM

OUTPUT

OUTPUT - MATRIX ADDITION
OUTPUT – MATRIX ADDITION

Leave a Reply

Your email address will not be published.