The linear equations in a matrix form are \Large A \cdot X = B and we want to find the values of X. You can solve it in many ways, and one of the simplest ways to solve \Large A \cdot X = B system of equations is Gauss elimination method. It is also known as Reduction method.

This example program solves any kind of linear equation of matrix form using Gauss elimination method.

Problem Definition

This program asks users to input the number of equations in the system of linear equations. The system of the equation looks like the following if the number of an equation is \Large 3.

Example,

\Large \begin{aligned}&x_1 + x_2 + x_3 = 2\\ \\
&2x_1 - 3x_2 + 2x_3 = -6 \\ \\
&x_1 + x_2 - 3x_3 = 6 
\end{aligned}

The same system of equation in \Large A \cdot X = B form will be

\Large \begin{bmatrix}1 & 1 & 1 \\2& -3 & 2  \\ 1&1& -3 \end{bmatrix} \cdot  \begin{bmatrix}x_1 \\ x_2\\ x_3\end{bmatrix} = \begin{bmatrix}2\\-6\\6\end{bmatrix}

You want to find the solution to \Large x_1, x_2 and \Large x_3using the Gauss elimination method. The Gauss elimination method is done using a series of row and column operations on the coefficient matrix. The coefficient matrix must be a square matrix otherwise the equation will not work.

For example, if we perform a series of row operation on the above matrix.

\Large \begin{aligned}
&R_2 \rightarrow R_2 - 3R_1\\
&R_3 \rightarrow R_3 - R_1
\end{aligned}

where \Large R_2 is row2 and \Large R_3 is row3.

You get an upper triangular matrix as shown below. The equation is in the form \Large A \cdot X = B.

\Large \begin{bmatrix}1 & 1 & 1 \\0&-5&0\\0&0& -4 \end{bmatrix} \cdot  \begin{bmatrix}x_1 \\ x_2\\ x_3\end{bmatrix} = \begin{bmatrix}2\\-10\\4\end{bmatrix}

Note that whatever operations performed on \Large A will also be performed on \Large B. Finally, by “back substitution”, you can find values of other solutions.

for example,

 \Large \begin{aligned}&-5x_2 = -10\\
& x_2 = 2\\\\
&x_1 + 2 - 1 = 2\\
&x_1 =1\\\\
&-4x_3=4\\
&x_3=-1\end{aligned}

Now we have \Large x_1 = 1 , x_2 = 2 and \Large x_3 = -1 as solution the system of equations.

Program Codes

/* Program to solve the given system of equations

x1 - x2 + x3 = 1
-3x1 + 2x2 - 3x3 = -6
2x1 - 5x2 + 4x3 = 5

We will use Gauss Elimination Method to solve it */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{

    int n,i,j,k;
    float a[10][10]={0.0}, x[10]={0.0};
    float pivot = 0.0;
    float factor = 0.0;
    float sum = 0.0;
    printf("Solution by Simple Gauss Elimination \n\n");

    printf("How many elements of the equations:");

    scanf("%d",&n);

    printf("%d\n",n);

    printf("\n\t Input Coefficients a[i, j+1], row-wise\n");

for(i=1;i<=n;i++)
{

    for(j=1;j<=n+1;j++)
    {
        scanf("%f",&a[i][j]);
    }

}
    printf("\n\n");
for(i=1;i<=n;i++)
{
    for(j=1;j<=n+1;j++)
    {

        printf("\t%10.0f",a[i][j]);

    }

    printf("\n\n");

}

for(k=1;k<=n-1;k++)
{

    if(a[k][k]==0.0)
    {

        printf("error");

    }
    else
    {
        pivot = a[k][k];
        for(j=k;j<=n+1;j++)
            a[k][j]= a[k][j]/pivot; 

for(i=k+1;i<=n;i++)
{
    factor = a[i][k];

    for(j = k;j<=n+1;j++)
    {
        a[i][j] = a[i][j] - factor * a[k][j];
    }
} 

} 

if(a[n][n]==0)

printf("error");

else
{

    x[n] = a[n][n+1]/a[n][n];

    for(i=n-1;i>=1;i--)
    {

        sum = 0.0;

        for(j=i+1;j<=n;j++)

        sum = sum + a[i][j] * x[j];

        x[i]= ( a[i][n+1]- sum )/a[i][i];

}

}

} 

for(i=1;i<=n;i++)
{

    printf("\n\tx[%1d]=%10.4f",i,x[i]); 

} 

    system("PAUSE"); 
    return 0;

}
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cout << "Number of variables: ";
    cin >> n;

    vector<vector<float>> a(n, vector<float>(n + 1));
    vector<float> x(n, 0.0);

    cout << "Enter augmented matrix row-wise (coefficients + constants):\n";
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n + 1; j++)
            cin >> a[i][j];

    // Forward elimination
    for (int k = 0; k < n - 1; k++) {
        if (a[k][k] == 0) {
            cout << "Error: Zero pivot element!\n";
            return 1;
        }
        for (int i = k + 1; i < n; i++) {
            float factor = a[i][k] / a[k][k];
            for (int j = k; j < n + 1; j++)
                a[i][j] -= factor * a[k][j];
        }
    }

    // Back-substitution
    for (int i = n - 1; i >= 0; i--) {
        float sum = 0.0;
        for (int j = i + 1; j < n; j++)
            sum += a[i][j] * x[j];
        x[i] = (a[i][n] - sum) / a[i][i];
    }

    cout << "\nSolution:\n";
    for (int i = 0; i < n; i++)
        cout << "x[" << i + 1 << "] = " << x[i] << "\n";

    return 0;
}
import java.util.Scanner;

class GaussElimination {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Number of variables
        int n = 3;
        float[][] a = new float[n][n + 1]; // augmented matrix
        float[] x = new float[n];          // solution vector

        System.out.println("Enter augmented matrix row-wise (coefficients + constants):");
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n + 1; j++)
                a[i][j] = sc.nextFloat();

        // Forward elimination
        for (int k = 0; k < n - 1; k++) {
            if (a[k][k] == 0) {
                System.out.println("Error: Zero pivot element!");
                return;
            }
            for (int i = k + 1; i < n; i++) {
                float factor = a[i][k] / a[k][k];
                for (int j = k; j < n + 1; j++)
                    a[i][j] -= factor * a[k][j];
            }
        }

        // Back-substitution
        for (int i = n - 1; i >= 0; i--) {
            float sum = 0;
            for (int j = i + 1; j < n; j++)
                sum += a[i][j] * x[j];
            x[i] = (a[i][n] - sum) / a[i][i];
        }

        // Print solution
        System.out.println("\nSolution:");
        for (int i = 0; i < n; i++)
            System.out.println("x[" + (i + 1) + "] = " + x[i]);

        sc.close();
    }
}
n = int(input("Number of variables: "))
a = []

print("Enter augmented matrix row-wise (coefficients + constants):")
for _ in range(n):
    row = list(map(float, input().split()))
    a.append(row)

x = [0.0 for _ in range(n)]

# Forward elimination
for k in range(n - 1):
    if a[k][k] == 0:
        print("Error: Zero pivot element!")
        exit()
    for i in range(k + 1, n):
        factor = a[i][k] / a[k][k]
        for j in range(k, n + 1):
            a[i][j] -= factor * a[k][j]

# Back-substitution
for i in range(n - 1, -1, -1):
    sum_ = 0
    for j in range(i + 1, n):
        sum_ += a[i][j] * x[j]
    x[i] = (a[i][n] - sum_) / a[i][i]

print("\nSolution:")
for i in range(n):
    print(f"x[{i+1}] = {x[i]:.4f}")

Output

   2             1             1            1
  
  -6             2            -3            2

   6             1             1           -3

       x[1]=     1.0000
       x[2]=     2.0000
       x[3]=    -1.0000 
Press any key to continue . . .