PROGRAM MatrixInverse
ALLOCATABLE :: A(:,:)
PRINT*,'ENTER THE SIZE OF THE MATRIX'
READ*,N
ALLOCATE (A(N,2*N))
A=0
PRINT*,'ENTER THE MATRIX'
READ*,((A(I,J),J=1,N),I=1,N)
DO 10 K=1,N,1
  A(K,N+K)=1
10 CONTINUE
!NOW A IS AN AGUMENTED MATRIX

DO 20 I=1,N,1
  DO 30 J=I+1,N,1
    !CHANGE PIVOT ELEMENT IF A(I,I)=0
    IF (.NOT.(ABS(A(I,I)).LT.1E-10)) GOTO 100 !DIGONAL ELEMENT IS NON ZERO
    IF (.NOT.(ABS(A(J,I)).LT.1E-10)) A(I,:)=A(I,:)+A(J,:)
  30 CONTINUE
  !IF THE ABOVE LOOP ENDS WITHOUT CHANGING PIVOT
  !IT IMPLIES ONE ENTIRE COLUMN IS ZERO COLUMN, SO INVERSE DOESN'T EXIST
  IF (ABS(A(I,I)).LT.1E-10) THEN
  PRINT*,'INVERSE DOES NOT EXIST'
  STOP
  ENDIF
  
  100 A(I,:)=A(I,:)/A(I,I) !PIVOT ELEMENT=1
  DO 40 K=1,N,1
    IF (K.NE.I) A(K,:)=A(K,:)-(A(K,I)*A(I,:))
  40 CONTINUE
20 CONTINUE

print*,'INVERSE OF THE MATRIX'
DO 50 I=1,N,1
  PRINT*,(A(I,J),J=(N+1),2*N)
50 CONTINUE

END PROGRAM MatrixInverse 
by

Fortran Online Compiler

Write, Run & Share Fortran code online using OneCompiler's Fortran online compiler for free. It's one of the robust, feature-rich online compilers for Fortran language, running on the latest version 7. Getting started with the OneCompiler's Fortran compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Fortran and start coding.

Read inputs from stdin

OneCompiler's Fortran online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Fortran program which takes name as input and prints hello message with your name.

program hello
  character :: name*30
  read *, name
	print *, "Hello ", name
end program hello

About Fortran

Fortran language was initially developed for scientific calculations by IBM in 1957. It has a number of in-built functions to perform mathematical calculations and is ideal for applications which has more mathematical calculations.

Syntax help

Data Types

Data typeDescriptionUsage
IntegerTo store integer variablesinteger :: x
RealTo store float valuesreal :: x
ComplexTo store complex numberscomplex :: x,y
LogicalTo store boolean values True or falselogical :: x=.True. , logical :: x = .FALSE.
CharacterTo store characters and stringscharacter :: x

Variables

Variable is a name given to the storage area in order to manipulate them in our programs.

data type :: variable_name

Arrays

Array is a collection of similar data which is stored in continuous memory addresses.

Syntax

data-type, dimension (x,y) :: array-name

Example

integer, dimension(3,3) :: cube

Loops

1. Do:

Do is used to execute a set of statement(s) iteratively when a given condition is true and the loop variable must be an integer.

do i = start, stop [,step]    
   ! code
end do

2. Do-While:

Do-While is used to execute a set of statement(s) iteratively when a given condition is true.

do while (condition) 
   !Code
end do

3. If:

If is used to execute a set of statements based on a condition.

if (logical-expression) then      
   !Code  
end if

4. If-Else:

If is used to execute a set of statements based on a condition and execute another set of statements present in else block, if condition specified in If block fails.

if (logical-expression) then     
   !code when the condition is true
else
   !code when the condition fails
end if

5. Case:

Case is similar to switch in C language.

[name:] select case (regular-expression) 
   case (value1)          
   ! code for value 1          
   ... case (value2)           
   ! code for value 2           
   ...       
   case default          
   ! default code          
   ...   
end select [name]