# Simultaneous equation using `gauss elimination method`

Program:

``````#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, n;
float a, x;
double s, p ;
clrscr();
printf("Enter the number of equations : ");
scanf("%d", &n);
printf("\nEnter the coefficients of the equations :\n\n");
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf("a[%d][%d] = ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
printf("b[%d] = ", i + 1);
scanf("%f", &a[i][n]);
}
for(k = 0 ; k < n - 1 ; k++)
{
for(i = k + 1 ; i < n ; i++)
{
p = a[i][k] / a[k][k];
for(j = k ; j < n + 1 ; j++)
a[i][j] = a[i][j] - p * a[k][j];
}
}
x[n-1] = a[n-1][n] / a[n-1][n-1];
for(i = n - 2 ; i >= 0 ; i--)
{
s = 0;
for(j = i + 1 ; j < n ; j++)
{
s  = (a[i][j] * x[j]);
x[i] = (a[i][n] - s) / a[i][i];
}
}
printf("\nThe result is :\n");
for(i = 0 ; i < n ; i++)
printf("\nx[%d] = %.2f", i + 1, x[i]);
getch();
}``````

Output:

``````Enter the number of equations : 3
Enter the coefficients of the equations :
a = 10
a = 1
a = 1
b = 12
a = 2
a = 10
a = 1
b = 13
a = 1
a = 1
a = 5
b = 7
The result is :
x = 1.00
x = 1.00
x = 1.00``````
• Created
Oct 15, 2014
• Updated
Dec 30, 2019
• Views
5,404
You May Like