You are on page 1of 3

Date: 29-06-21

<<<<<<<<<<<< Gauss’s Elimination Method >>>>>>>>>>>>>


Roll Number : SH-157
• STATEMENT OF THE PROBLEM:
Write a C program to solve the following system of equations by Gauss
elimination method correct up to five decimal places.
4.87 2.39 1.14 2.22 𝑥1 4.11
1.69 5.43 2.48 1.12 𝑥2 2.10
൮ ൲൮ ൲=൮ ൲
3.10 0.89 6.15 1.36 𝑥3 4.25
0.87 2.30 1.90 4.86 𝑥4 3.15

SOLUTION:
#include<stdio.h>
void main()
{
float a[11][11],b[11],x[11],m,sum;
int n,i,j,k;
printf("Enter the number of unknown\n");
scanf("%d",&n);
printf("Enter the coefficient matrix \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%f",&a[i][j]);
}
printf("\nEnter the constant matrix\n");
for(i=1;i<=n;i++)
scanf("%f",&b[i]);
for(j=1;j<n;j++)
{
for(i=j+1;i<=n;i++)
{
m=-a[i][j]/a[j][j];
for(k=1;k<=n;k++)
a[i][k]=a[i][k]+m*a[j][k];
b[i]=b[i]+m*b[j];
}
}
for(i=n;i>=1;i--)
{
sum=0;
for(j=i+1;j<=n;j++)
sum=sum+a[i][j]*x[j];
x[i]=(b[i]-sum)/a[i][i];
}
printf("\nThe solution is\n");
for(i=1;i<=n;i++)
printf("x[%d]=%8.5f\n",i,x[i]);
printf("(correct up to five decimal places)\n");
}
OUTPUT:
Enter the number of unknown
4
Enter the coefficient matrix
4.87 2.39 1.14 2.22
1.69 5.43 2.48 1.12
3.10 0.89 6.15 1.36
0.87 2.30 1.90 4.86

Enter the constant matrix


4.11
2.10
4.25
3.15

The solution is
x[1]= 0.58594
x[2]=-0.02384
x[3]= 0.30269
x[4]= 0.43620
(correct up to five decimal places)
----------------------------

You might also like