Java Program to Represent Linear Equations in Matrix Form

«
»
This is java program to convert the system of linear equations to matrix form. The input is the coefficient of each variable and constant. Class rearranges them and converts them into matrix form, which aids solving them.

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

  1. //This is a sample program to represent linear equations into matrix from
  2. import java.util.Scanner;
  3.  
  4. public class Matrix_Representation_Equations 
  5. {
  6.     public static void main(String args[])
  7.     {
  8.         char []var = {'x', 'y', 'z', 'w', 'a', 'b', 'c', 'd', 'e'};
  9.         System.out.println("Enter the number of variables in the equations: ");
  10.         Scanner input = new Scanner(System.in);
  11.         int n = input.nextInt();
  12.         System.out.println("Enter the coefficients of each variable for each equations");
  13.         System.out.println("ax + by + cz + ... = d");
  14.         int [][]mat = new int[n][n];
  15.         int [][]constants = new int[n][1];
  16.         for(int i=0; i<n; i++)
  17.         {
  18.             for(int j=0; j<n; j++)
  19.             {
  20.                 mat[i][j] = input.nextInt();
  21.             }
  22.             constants[i][0] = input.nextInt();
  23.         }
  24.  
  25.         System.out.println("Matrix representation is: ");
  26.         for(int i=0; i<n; i++)
  27.         {
  28.             for(int j=0; j<n; j++)
  29.             {
  30.                 System.out.print(" "+mat[i][j]);
  31.             }
  32.             System.out.print("  "+ var[i]);
  33.             System.out.print("  =  "+ constants[i][0]);
  34.             System.out.println();
  35.         }
  36.         input.close();
  37.     }
  38. }

Output:

advertisement
$ javac Matrix_Representation_Equations.java
$ java Matrix_Representation_Equations
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
5 6 7 8 
9 0 1 2 
Matrix representation is: 
 1 2 3  x  =  4
 5 6 7  y  =  8
 9 0 1  z  =  2

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java 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 & technical discussions at Telegram SanfoundryClasses.