9 Nov 2018, 9:00 AM
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,k,n;
float A[30][30],L[30][30],U[30]
[30],x[30],y[30],b[30],s,t,a,c;
printf("Enter the order of the coefficient
matrix\n");
scanf("%d",&n);
printf("Enter the element of coefficient matrix
row wise\n");
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%f",&A[i][j]);
}
}
printf("Enter the column vector\n");
for(i=1;i<=n;i++){
scanf("%f",&b[i]);
}
for(i=1;i<=n;i++){
L[i][i]=1;
for(j=1;j<=n;j++){
if(i<j){
L[i][j]=0;
}
if(i>j){
U[i][j]=0;
}
}
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i<=j){
s=0;
for(k=1;k<=i-1;k++){
s=s+L[i][k]*U[k][j];
}
U[i][j]=A[i][j]-s;
}
if(i>j){
t=0;
for(k=1;k<=j-1;k++){
t=t+L[i][k]*U[k][j];
}
L[i][j]=(A[i][j]-t)/U[j][j];
}
}
}
y[1]=b[1];
for(i=2;i<=n;i++){
a=0;
for(j=1;j<=i-1;j++){
a=a+L[i][j]*y[j];
}
y[i]=b[i]-a;
}
x[n]=y[n]/U[n][n];
for(i=n-1;i>=1;i--){
c=0;
for(j=i+1;j<=n;j++){
c=c+U[i][j]*x[j];
}
x[i]=(y[i]-c)/U[i][i];
}
printf("The required solution is\n");
for(i=1;i<=n;i++){
printf("x[%d]=%6.4f\n",i,x[i]);
}
getch();
}