C Program to Represent Linear Equations in Matrix Form

This is a C Program to represent a set of linear equations in matrix form. This is c program to convert the system of linear equations to matrix form. The input is the coefficient of each variable and constant. Program rearranges them and converts them into matrix form, which aids solving them.

Here is source code of the C Program to Represent Linear Equations in Matrix Form. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void) {
  4.     char var[] = { 'x', 'y', 'z', 'w' };
  5.     printf("Enter the number of variables in the equations: ");
  6.     int n;
  7.     scanf("%d", &n);
  8.     printf("\nEnter the coefficients of each variable for each equations");
  9.     printf("\nax + by + cz + ... = d");
  10.     int mat[n][n];
  11.     int constants[n][1];
  12.     int i,j;
  13.     for (int i = 0; i < n; i++) {
  14.         for (int j = 0; j < n; j++) {
  15.             scanf("%d", &mat[i][j]);
  16.         }
  17.         scanf("%d", &constants[i][0]);
  18.     }
  19.  
  20.     printf("Matrix representation is: ");
  21.     for (i = 0; i < n; i++) {
  22.         for (j = 0; j < n; j++) {
  23.             printf(" %f", mat[i][j]);
  24.         }
  25.         printf(" %f", var[i]);
  26.         printf(" = %f", constants[i][0]);
  27.         printf("\n");
  28.     }
  29.     return 0;
  30. }

Output:

$ gcc MatRepOfEquation.cpp
$ ./a.out
 
Enter the number of variables in the equations: 3
 
Enter the coefficients of each variable for each equations
ax + by + cz + ... = d
1 2 3 4
1 2 6 8
2 3 9 8
Matrix representation is:  
 1 2 3  x  =  4
 1 2 6  y  =  8
 2 3 9  z  =  8

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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.