program mat_mul_sub
implicit none
INTEGER::i,j,m,n,p,q
REAL, DIMENSION(10,10)::mat1,mat2,res
PRINT *, '============================================'
PRINT *, 'MATRIX MULTIPLICATION USING SUBROUTINE [BOTTOMSCIENCE.COM]'
PRINT *, '============================================'
print *, '*** Matrix Multiplication ***'
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
call matmul_sub(mat1,mat2,res,m,n,q)
do i=1,m
write(*,*) (res(i,j),j=1,q)
end do
else
print *,'Can''t multiply'
end if
end program
!SUBROUTINE
subroutine matmul_sub(mat11,mat22,res1,m1,n1,q1)
INTEGER::i1,j1,m1,n1,q1,k1
REAL::s1
REAL, DIMENSION(10,10)::mat11,mat22,res1
do i1=1,m1
do j1=1,q1
s1=0
do k1=1,n1
s1=s1+mat11(i1,k1)*mat22(k1,j1)
end do
res1(i1,j1)=s1
end do
end do
end subroutine
OUTPUT
============================================
MATRIX MULTIPLICATION USING SUBROUTINE [BOTTOMSCIENCE.COM]============================================
*** Matrix Multiplication ***
For Matrix 1
Enter the number of rows
2
Enter the number of columns
2
Enter the elements
1
2
3
4
For Matrix 2
Enter the number of rows
2
Enter the number of columns
2
Enter the elements
1
2
3
4
7.00000000 10.0000000
15.0000000 22.0000000