Pages

Wednesday

Simultaneous Equation using Gauss Elimination Method in C | C Program

Here's a C program for solving simultaneous equation using Gauss elimination method using for loops with output. This program uses Array, For Loops and Nested Loops.


# include <stdio.h>
# include <conio.h>
void main() 
{ 
 int i, j, k, n ; 
 float a[20][20], x[20] ; 
 double s, p ; 
 clrscr() ; 
 printf("Enter the number of equations : ") ; 
 scanf("%d", &n) ; 
 printf("\nEnter the co-efficients of the equations :\n\n") ; 
 for(i = 0 ; i < n ; i++) 
 { 
  for(j = 0 ; j < n ; j++) 
  { 
   printf("a[%d][%d] = ", i + 1, j + 1) ; 
   scanf("%f", &a[i][j]) ; 
  } 
  printf("b[%d] = ", i + 1) ; 
  scanf("%f", &a[i][n]) ; 
 } 
 for(k = 0 ; k < n - 1 ; k++) 
 { 
  for(i = k + 1 ; i < n ; i++) 
  { 
   p = a[i][k] / a[k][k] ; 
   for(j = k ; j < n + 1 ; j++) 
    a[i][j] = a[i][j] - p * a[k][j] ; 
  } 
 } 
 x[n-1] = a[n-1][n] / a[n-1][n-1] ; 
 for(i = n - 2 ; i >= 0 ; i--) 
 { 
  s = 0 ; 
  for(j = i + 1 ; j < n ; j++) 
  { 
   s += (a[i][j] * x[j]) ; 
   x[i] = (a[i][n] - s) / a[i][i] ; 
  } 
 } 
 printf("\nThe result is :\n") ; 
 for(i = 0 ; i < n ; i++) 
  printf("\nx[%d] = %.2f", i + 1, x[i]) ; 
 getch() ; 
}

Output of above program is

Enter the number of equations : 3
                                                                     
Enter the co-efficients of the equations :                           
                                                                     
a[1][1] = 10                                                         
a[1][2] = 1                                                          
a[1][3] = 1                                                          
b[1] = 12                                                            
a[2][1] = 2                                                          
a[2][2] = 10                                                         
a[2][3] = 1                                                          
b[2] = 13                                                            
a[3][1] = 1
a[3][2] = 1                                                          
a[3][3] = 5                                                          
b[3] = 7                                                             
                                                                     
The result is :                                                      
                                                                     
x[1] = 1.00                                                          
x[2] = 1.00                                                          
x[3] = 1.00

1 comment:

  1. Hello There,


    Brilliant article, glad I slogged through the C Programming Tutorial it seems that a whole lot of the details really come back to from my past project.

    write a c program that finds number of any paragraph and sentence in a text.
    I'm new at this and I really need to do this.


    Follow my new blog if you interested in just tag along me in any social media platforms!


    Thank you,
    Heena

    ReplyDelete