Gauss-Seidel(Iterative Method) For System of Linear Equations-C++ Program

So I wrote this piece of code for solving a system of linear equations using Gauss-Seidel’s Iterative method in the fifth semester of my undergraduate course for my Numerical Analysis Class. Hope you guys find it useful.

//Gaus-seidel (Written by: Manas Sharma - University of Delhi)
using namespace std;
int main()
    int n,i,j,k,flag=0,count=0;
    cout<<"\nEnter the no. of equations\n";           
    cin>>n;                    //Input no. of equations
    double a[n][n+1];            //declare a 2d array for storing the elements of the augmented matrix
    double x[n];                //declare an array to store the values of variables
    double eps,y;
    cout<<"\nEnter the elements of the augmented matrix row-wise:\n";
    for (i=0;i<n;i++)
        for (j=0;j<=n;j++)
    cout<<"\nEnter the initial values of the variables:\n";
    for (i=0;i<n;i++)
    cout<<"\nEnter the accuracy upto which you want the solution:\n";
    for (i=0;i<n;i++)                    //Pivotisation(partial) to make the equations diagonally dominant
        for (k=i+1;k<n;k++)
            if (abs(a[i][i])<abs(a[k][i]))
                for (j=0;j<=n;j++)
                    double temp=a[i][j];
    do                            //Perform iterations to calculate x1,x2,...xn
        for (i=0;i<n;i++)                //Loop that calculates x1,x2,...xn
            for (j=0;j<n;j++)
                if (j!=i)
            if (abs(x[i]-y)<=eps)            //Compare the ne value with the last value
    }while(flag<n);                        //If the values of all the variables don't differ from their previious values with error more than eps then flag must be n and hence stop the loop
    cout<<"\n The solution is as follows:\n";
    for (i=0;i<n;i++)
        cout<<"x"<<i<<" = "<<x[i]<<endl;        //Print the contents of x[]
    return 0;
c++ program output iterative method gauss seidel
Sample Output

Sample Output
Sample Output

More Resources:
More programs on Numerical Analysis:
Lab Write-Up(with Algorithm and Flow-Chart):
Video Explaining the above code:

5 thoughts on “Gauss-Seidel(Iterative Method) For System of Linear Equations-C++ Program

    1. Hi Princeton University,
      Thanks for your comment. Actually I wrote that program for my Numerical Methods class. I am a Physics Postgraduate at University of Delhi.

      I have updated the above post now to include more resources.
      You might also want to check out this video of explanation of the above code on my YouTube Channel:
      The Lab-Write Up of the code which includes the algorithm and flow-chart:

      And more programs on Numerical Methods:

      Good Day!

  1. Hi Manas Sharma! Can you provide a version of this program in C language?

    1. Hi Rubik!
      C is not very different from C++ .
      I suppose all you need to do is replace the ‘cout< <' and 'cin>>’ statememts with the C equivalent ‘printf’ and ‘scanf’. Try to do that it’s not very hard. I am too busy to do it on my own.

Leave a Reply

Your email address will not be published. Required fields are marked *