C Program for Gauss-Seidel Method


The direct methods such as Cramer’s rule, matrix inversion method, Gauss Elimination method, etc. for the solution of simultaneous algebraic equations yield the solution after a certain amount of fixed computation. So, direct method of solution takes longer time to get the solution.

On the other hand, in case of iterative methods such as Gauss Jacobi and Gauss-Seidel iteration method, we start with an approximate solution of equation and iterate it till we don’t get the result of desired accuracy. Here is source code for Gauss-Seidel in C with working procedure and sample output.

The manual computation iterative method is quite lengthy. But, the program in high level languages run fast and effectively. This C program for Gauss-Seidel method has been designed for the solution of linear simultaneous algebraic equations based on the principle of iteration.

In this program, a certain approximate value of solution is assumed and further calculations are done based on the result of assumed approximate solution.

The program for Gauss-Seidel method in C works by following the steps listed below:

  • When the program is executed, first of all it asks for the value of elements of the augmented matrix row wise.
  • Then, the program asks for allowed error and maximum number of iteration to which the calculations are to be done. The number of iterations required depends upon the degree of accuracy.
  • The program assumes initial  or approximate solution as  y=0 and z=0 and new value of x which is used to calculate new values of y and z using the following expressions:

x= 1/a1 ( d1-b1y-c1z)
y=1/b2 ( d2-a2x-c2z)
z=1/c3 ( d3-a3x-b3y)

  • The iteration process is continued until a desired degree of accuracy is not met.

Source Code for Gauss-Seidel Method in C:


Gauss Seidel C Program Output
Also see,
Gauss Seidel Matlab Program
Gauss Seidel Algorithm/Flowchart
Numerical Methods Tutorial Compilation

In this C language code for Gauss-Seidel method, the value of order of square matrix has been defined as  a macro of value  2 which can be changed to any order in the source code.

Also, the elements of augmented matrix have been defined as array so that a number of values can be stored under a single variable name. The program can be used effectively to solve linear simultaneous algebraic equation though easy, accurate and convenient way.


  1. Hey, can if it’s not too much trouble can you look at my program & why it’s displaying results after only one iteration. I wrote it based on yours but i don’t have to read max no. of iterations or max. error allowed. I just have to read the no. of unknowns.

    main ()
    float a[20][20],x[10],max,t,s,e;
    int i,j,n,r;
    printf(“Enter the number of unknowns: “);
    scanf(“%d”, &n);
    printf(" Enter the elemrnts of augmented matrix rowwise\n");
    scanf("%f", &a[i][j]);
    printf("Answer obtained is:");
    printf("x[%d]=%f\n", i,x[i]);
    return 0;

  2. ok, may be this is just me being dumb or my faulty laptop but when i write your program it does not show errors but on execution it exits and i cannot see the result. plz check.

    • Hey Atul, I tested this program before posting it here, and it still executes on my computer. The source code is written and compiled in Code::Blocks.

      I don’t think the program would simply exit upon execution. Try writing a simple program to print “Hello World” to check if there’s any problem with your compiler.


Please enter your comment!
Please enter your name here