- HubPages
*»* - Technology
*»* - Computers & Software
*»* - Computer Science & Programming

# C Program for Gauss Elimination Method to Solve Linear Equations

We all know that Gauss elimintaion method used in mathematics is easier to find the solution of linear equations . Following is the source code for the same method . Just copy the code in your notepad and save it with the extension ***.c **or ***.cpp**. Then open the saved file with your Compiler and run it .

#include<stdio.h>

#include<conio.h>

int main()

{

int i,j,k,n;

float a[10][10],c[10],x[10];

printf("****** welcome 2 gauss elimination method *******\n");

printf("\nenter the number of elements: ");

scanf("%d",&n);

printf("Enter constants: ");

for(i=0;i<n;i++)

scanf("%f",&c[i]);

printf("\nenter the matrix: ");

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

for(j=0;j<n;j++)

scanf("%f",&a[i][j]);

}

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

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

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

a[i][j]=a[i][j]-(a[i][k]/a[k][k])*a[k][j];

c[i]=c[i]-(a[i][k]/a[k][k])*c[k];

}

}

x[n-1]=c[n-1]/a[n-1][n-1];

printf("The solution is: \n");

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

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

{

i=n-k-2;

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

c[i]=c[i]-(a[i][j]*x[j]);

x[i]=c[i]/a[i][i];

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

}

getch();

}

}

After running the program , you need to enter the constants as well as coefficient of the three linear equations in the form of matrix . The program thereafter will give you desired result . :)

10