title: Gaussian Elimination in C
body: <p>This code works only if the system has unique solutions. When there is no solution or infinitely many solutions, it should print "Has no unique solution." but the code below prints "nan" or "inf". How can I do that?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  int main()
  {
  int i,j,k,n;
  double A[20][20],c,x[10];
  printf("\nEnter the size of matrix: ");
  scanf("%d",&amp;n);
  printf("\nEnter the elements of augmented matrix row-wise:\n");
  for(i=1; i&lt;=n; i++)
  {
      for(j=1; j&lt;=(n+1); j++)
      {
          printf(" A[%d][%d]:", i,j);
          scanf("%lf",&amp;A[i][j]);
      }
  }
  
  for(j=1; j&lt;=n; j++)
  {
      for(i=1; i&lt;=n; i++)
      {
          if(i!=j)
          {
              c=A[i][j]/A[j][j];
              for(k=1; k&lt;=n+1; k++)
              {
                  A[i][k]=A[i][k]-c*A[j][k];
              }
          }
      }
  }
  printf("\nThe solution is:\n");
  for(i=1; i&lt;=n; i++)
  {
      x[i]=A[i][n+1]/A[i][i];
      printf("\n x%d=%0.3f\n",i,x[i]);
  }
  return(0);
  }
  </code></pre>