0% found this document useful (0 votes)
8 views4 pages

MatrixAddition and Multiplication

The document contains two Fortran programs for matrix operations: one for matrix addition and another for matrix multiplication. Each program prompts the user to input the dimensions and values of two matrices, performs the respective operation if the dimensions are compatible, and then outputs the resulting matrix. If the dimensions do not allow for the operation, an error message is displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

MatrixAddition and Multiplication

The document contains two Fortran programs for matrix operations: one for matrix addition and another for matrix multiplication. Each program prompts the user to input the dimensions and values of two matrices, performs the respective operation if the dimensions are compatible, and then outputs the resulting matrix. If the dimensions do not allow for the operation, an error message is displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

!!!!

This program first insert some vulues for Matrix A


!!!!Then print the Matrix A

!!!!Similiar to the Matrix A, insert some vulues for Matrix B


!!!!Then print the Matrix B

!!!!! Then calculate Summation Matrix C by adding Matrix A and Matrix B


!!!!!Then print the Summation Matrix C
program Matrix_Addition
implicit none
integer::i,j,m,n,p,q
real::a(100,100), b(100,100), c(100,100)

write(*,*) "Number of row for A matrix:"


read(*,*) m
write(*,*) "Number of column for A matrix:"
read(*,*) n

write(*,*) "Insert the value of Matrix A:"


do i=1,m,1
do j=1,n
read(*,*) a(i,j)
end do
end do

write(*,*) "Matrix A="


do i=1,m,1
! write(*,*) (a(i,j),j=1,n)
write(*,5) (a(i,j),j=1,n)
5 format(10x,100(f6.1,5x))
end do

write(*,*) "Number of row for B Matrix:"


read(*,*) p
write(*,*) "Number of column for B Matrix:"
read(*,*) q
write(*,*) "Insert the value of Matrix B:"
do i=1,p,1
do j=1,q
read(*,*) b(i,j)
end do
end do

write(*,*) "Matrix B="


do i=1,p,1
write(*,5) (b(i,j),j=1,q)
end do

if (m==p .and. n==q) then


write(*,*) "Calculate the Summation Matrix C by adding Matrix A and Matrix B:"
do i=1,m,1
do j=1,n
c(i,j)=a(i,j)+ b(i,j)
end do
end do

write(*,*) "Summation Matrix C="

do i=1,m,1
write(*,5) (c(i,j),j=1,n)
end do

else
write(*,*) " Addition of A and B matrix is not possible!"
end if
end program Matrix_Addition

program Matrix_Multiplication
implicit none
integer::i,j,m,n,p,q,k
!real::a(100,100), b(100,100), c(100,100)
real, dimension(100,100):: a,b,c

write(*,*) "Number of row:"


read(*,*) m
write(*,*) "Number of column:"
read(*,*) n

write(*,*) "Insert the value of Matrix A:"


do i=1,m,1
do j=1,n
read(*,*) a(i,j)
end do
end do

write(*,*) "Matrix A="


do i=1,m,1
write(*,5) (a(i,j),j=1,n)
5 format(10x,100(f4.1,5x))
end do

write(*,*) "Number of row for B Matrix:"


read(*,*) p
write(*,*) "Number of column for B Matrix:"
read(*,*) q

write(*,*) "Insert the value of Matrix B:"


do i=1,p,1
do j=1,q,1
read(*,*) b(i,j)
end do
end do

write(*,*) "Matrix B="

do i=1,p,1
write(*,5) (b(i,j),j=1,q)
end do

if (n==p) then
write(*,*) "Calculate the Matrix C of order (m by q) by multiplying Matrix A and Matrix B:"
do i=1,m,1
do j=1,q
c(i,j) = 0
do k=1,n
c(i,j)=c(i,j)+a(i,k)*b(k,j)
end do
end do
end do

write(*,*) "Multiplication Matrix C="


do i=1,m,1
write(*,5) (c(i,j),j=1,q)
end do

else
write(*,*) "Matrix multiplication (AB) of A and B is not possible!"
end if
end program Matrix_Multiplication

You might also like