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

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.