# Gauss Jordan Method Python Program (With Output)

This python program solves systems of linear equation with n unknowns using Gauss Jordan Method.

In Gauss Jordan method, given system is first transformed to Diagonal Matrix by row operations then solution is obtained by directly.

## Gauss Jordan Python Program

``````
# Importing NumPy Library
import numpy as np
import sys

n = int(input('Enter number of unknowns: '))

# Making numpy array of n x n+1 size and initializing
# to zero for storing augmented matrix
a = np.zeros((n,n+1))

# Making numpy array of n size and initializing
# to zero for storing solution vector
x = np.zeros(n)

print('Enter Augmented Matrix Coefficients:')
for i in range(n):
for j in range(n+1):
a[i][j] = float(input( 'a['+str(i)+']['+ str(j)+']='))

# Applying Gauss Jordan Elimination
for i in range(n):
if a[i][i] == 0.0:
sys.exit('Divide by zero detected!')

for j in range(n):
if i != j:
ratio = a[j][i]/a[i][i]

for k in range(n+1):
a[j][k] = a[j][k] - ratio * a[i][k]

# Obtaining Solution

for i in range(n):
x[i] = a[i][n]/a[i][i]

# Displaying solution
print('\nRequired solution is: ')
for i in range(n):
print('X%d = %0.2f' %(i,x[i]), end = '\t')
``````

Output

```Enter number of unknowns: 3
Enter Augmented Matrix Coefficients:
a=1
a=1
a=1
a=9
a=2
a=-3
a=4
a=13
a=3
a=4
a=5
a=40

Required solution is:
X0 = 1.00	X1 = 3.00	X2 = 5.00
```