Thursday, 2 November 2017

C Programme for Gauss Seidal Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

float x[15],a[15][15],t,sum;
int i,j,n,itr,maxitr;
clrscr();
printf("\n enter the size of matrix:");
scanf("%d",&n);
printf("\nenter matrix element\n:");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
scanf("%f",&a[i][j]);
}
printf("Enter maximum iteration:");
scanf("%d",&maxitr);
for(i=1;i<=n;i++)
{
x[i]=0;
}
for(itr=1;itr<=maxitr;itr++)
{
for(i=0;i<=n;i++)
{
sum=0.0;
for(j=1;j<=n;j++)
{
if(j!=i)
sum=sum+a[i][j]*x[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
printf("Roots after iteration%d\n ",itr);
for(i=1;i<=n;i++)
printf("x%d=%f\n",i,x[i]);
}
getch();
}

Input/Output:




No comments:

Post a Comment