Skip to main content

C Program for Gauss Elimination Method

C Program for Gauss Elimination Method
#include<stdio.h>
#include<conio.h>

void main()
{
  int i,j,k,n;
  float A[20][20],c,x[10],sum;
  printf("\nEnter the order of matrix: ");
  scanf("%d",&n);
  printf("\nEnter the elements of augmented matrix row-wise:\n\n");
  for(i=1; i<=n; i++)
  {
    for(j=1; j<=(n+1); j++)
    {
      printf("A[%d][%d] : ", i,j);
      scanf("%f",&A[i][j]);
    }
  }

  /* loop for the generation of upper triangular matrix*/
  for(i=1; i<=n; i++)
  {
    for(j=1; j<=n; j++)
    {
      if(j>i)
      {
        c=A[j][i]/A[i][i];
        for(k=1; k<=n+1; k++)
        {
          A[j][k]=A[j][k]-c*A[i][k];
        }
      }
    }
  }

  /* Upper Traingular matrix */
  printf("\nThe Upper Triangular matrix is: \n\n");
  for(i=1; i<=n; i++)
  {
    for(j=1; j<=(n+1); j++)
    {
      printf("%f ",A[i][j]);
    }
    printf("\n");
  }

  /* initializing x[i] to zeros */
  for(i=1; i<=n; i++)
  {
    x[i]=0;
  }

  /* loop is for backward substitution */
  printf("\nAfter applying Backward Substitution: \n");
  for(i=n; i>=1; i--)
  {
    sum=0;
    for(j=1; j<=n; j++)
    {
      if(i!=j)
        sum=sum+A[i][j]*x[j];
    }
    x[i]=(A[i][n+1]-sum)/A[i][i];
  }

  printf("\nThe solution is: \n");
  for(i=1; i<=n; i++)
  {
    printf("\nx%d=%f\t",i,x[i]);
    /* x1, x2, x3 are the required solutions */
  }
  getch();
}

Comments

Popular posts from this blog

C Program for Runge-Kutta-4 (RK-4) Method

Program for Runge-Kutta-4 (RK-4) Method #include <stdio.h> #include <conio.h> #include <math.h> float f(float x,float y) {   return ((y*y-x*x)/(y*y+x*x));   //y'=f(x,y)=equation } void main() {   float x0,y0,h,xn,yn;   printf("Enter x0 and y0: ");   scanf("%f%f",&x0,&y0); //y(x0)=y0   printf("Enter xn: ");   scanf("%f",&xn);   printf("Enter interval(h): ");   scanf("%f",&h);   do   {     float m1=f(x0,y0);     float m2=f(x0+h/2,y0+m1*h/2);     float m3=f(x0+h/2,y0+m2*h/2);     float m4=f(x0+h,y0+m3*h);     float m=(m1+2*m2+2*m3+m4)/6;     yn=y0+m*h;     //for next iteration         x0=x0+h;     y0=yn;   }while(x0<xn); printf("\n\nHence, y(%0.1f)=%0.4f",xn,yn); getch(); }