----------------------------------------------------------------------------------------------------------------------------------------
PROGRAM gauss
IMPLICIT NONE
INTEGER::n,i,j,j1,i2,i3,j3,i4,k
REAL::landa,s
REAL,ALLOCATABLE::a(:,:),x(:),b(:)
PRINT*
PRINT*,"matrix n dar n+1 zir ra dar nazar begirid::"
PRINT*
PRINT*," _ _ "
PRINT*," 1 | a(1,1) a(1,2) ... a(1,n) | a(1,n+1) | "
PRINT*," 2 | a(2,1) a(2,2) ... a(2,n) | a(2,n+1) | "
PRINT*," 3 | a(3,1) a(3,2) ... a(3,n) | a(3,n+1) | "
PRINT*," . | . . . . | . | "
PRINT*," . | . . . . | . | "
PRINT*," . | . . . . | . | "
PRINT*," . | . . . . | . | "
PRINT*," . | . . . . | . | "
PRINT*," n |_ a(n,1) a(n,2) ... a(n,n) | a(n,n+1) _| "
PRINT*," (n,n+1)"
PRINT*," ------------------------- -----------"
PRINT*," matris zarayeb bordar ma-loom"
PRINT*
PRINT*,"__________________________________________________________________"
PRINT*
PRINT*,"lotafan tedad moadelat ya (n) ra vared konid:"
READ*,n
ALLOCATE (a(n,n+1),x(n),b(n+1))
DO k=1,n
PRINT*,"khate",k,"ra vared konid ( az a(",k,", 1) ta a(",k,",",n+1,") ) :"
READ*,a(k,:)
END DO
PRINT*,"-----------------------------------------------------------------"
DO i=1,n
IF (a(i,i)==0) THEN
DO j=i+1,n
IF (a(j,i)/=0) THEN
b=a(j,:)
a(j,:)=a(i,:)
a(i,:)=b
END IF
END DO
END IF
DO j1=i+1,n
landa=-a(j1,i)/a(i,i)
a(j1,:)=(landa*a(i,:))+a(j1,:)
END DO
END DO
PRINT*
PRINT*
x(n)=a(n,n+1)/a(n,n)
DO i3=n-1,1,-1
s=0.
DO j3=i3+1,n
s=s+(a(i3,j3)*x(j3))
END DO
x(i3)=(a(i3,n+1)-s)/a(i3,i3)
END DO
DO i4=1,n
PRINT*," x(",i4,") = ",x(i4)
END DO
PRINT*
PRINT*
PRINT*,"__________________________________________________________"
END