C Program to Implement Gauss Jordan Elimination

This C program implements Gauss Jordan Elimination method. In linear algebra, Gaussian elimination is an algorithm for solving systems of linear equations.

Here is the source code of the C program to find solution of a system of linear equations. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2.  
  3. void solution( int a[][20], int var );
  4. int main()
  5. {
  6.  
  7.     int a[ 20 ][ 20 ], var, i, j, k, l, n;
  8.     printf( "\nEnter the number of variables:\n" );
  9.     scanf( "%d", &var );
  10.  
  11.     for ( i = 0;i < var;i++ )
  12.     {
  13.         printf( "\nEnter the equation%d:\n", i + 1 );
  14.  
  15.         for ( j = 0;j < var;j++ )
  16.         {
  17.             printf( "Enter the coefficient of  x%d:\n", j + 1 );
  18.             scanf( "%d", &a[ i ][ j ] );
  19.         }
  20.  
  21.         printf( "\nEnter the constant:\n" );
  22.         scanf( "%d", &a[ i ][ var] );
  23.     }
  24.  
  25.     solution( a, var );
  26.     return 0;
  27. }
  28.  
  29.  
  30.  
  31. void solution( int a[ 20 ][ 20 ], int var )
  32. {
  33.     int k, i, l, j;
  34.  
  35.     for ( k = 0;k < var;k++ )
  36.     {
  37.         for ( i = 0;i <= var;i++ )
  38.         {
  39.             l = a[ i ][ k ];
  40.  
  41.             for ( j = 0;j <= var;j++ )
  42.             {
  43.                 if ( i != k )
  44.                 a[i][j] = (a[k][k]*a[i][j])-(l*a[k][j]);
  45.             }
  46.         }
  47.     }
  48.  
  49.     printf( "\nSolutions:" );
  50.  
  51.     for ( i = 0;i < var;i++ )
  52.     {
  53.         printf( "\nTHE VALUE OF x%d IS %f\n", i + 1, ( float ) a[ i ][ var ] / ( float ) a[ i ][ i ] );
  54.     }
  55.  
  56. }

$ gcc bubblesort.c -o bubblesort
$ ./bubblesort
 
Enter the number of variables: 3
 
Enter the equation 1:
Enter the coefficient of  x1: 1 
Enter the coefficient of  x2: 0
Enter the coefficient of  x3: 0
 
Enter the constant: 2
 
Enter the equation 2:
Enter the coefficient of  x1: 0
Enter the coefficient of  x2: 1 
Enter the coefficient of  x3: 0
 
Enter the constant: 0
 
Enter the equation 3:
Enter the coefficient of  x1: 0
Enter the coefficient of  x2: 0
Enter the coefficient of  x3: 1
 
Enter the constant: -1
 
Solutions:
THE VALUE OF x1 IS 2.000000
 
THE VALUE OF x2 IS 0.000000
 
THE VALUE OF x3 IS -1.000000

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.