Java Program to Solve Any Linear Equation in One Variable

«
»
This is a java program to solve a linear equation in one variable.

Here is the source code of the Java Program to Solve any Linear Equation in One Variable. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find the solution to the linear equation in single variable
  2. import java.util.Scanner;
  3.  
  4. public class LEquation
  5. {
  6.     public static void main(String args[])
  7.     {
  8.         String eqn = "";
  9.         float ans = 0;
  10.         float coeffSum = 0;
  11.         float constSum = 0;
  12.         float coeffx[] = new float[100];
  13.         float[] constant = new float[100];
  14.         Scanner in = new Scanner(System.in);
  15.         System.out.println("Enter a linear equation\n");
  16.         eqn = in.nextLine();
  17.         eqn += "\n";
  18.         // System.out.println(eqn);
  19.         for (int i = 0, j = 0, k = 0; i < eqn.length() - 1;)
  20.         {
  21.             if (eqn.charAt(i + 1) == 'x' && i < eqn.indexOf("="))
  22.             {
  23.                 if (i != 0 && eqn.charAt(i - 1) == '-')
  24.                 {
  25.                     String x = eqn.substring(i, i + 1);
  26.                     if (x != "+" && x != "-")
  27.                     {
  28.                         int n = -(Integer.parseInt(x, 10));
  29.                         coeffx[j++] = n;
  30.                     }
  31.                 } else
  32.                 {
  33.                     String x = eqn.substring(i, i + 1);
  34.                     if (x != "+" && x != "-")
  35.                     {
  36.                         int n = Integer.parseInt(x, 10);
  37.                         coeffx[j++] = n;
  38.                     }
  39.                 }
  40.                 i += 3;
  41.             }
  42.             if (eqn.charAt(i + 1) == 'x' && i > eqn.indexOf("="))
  43.             {
  44.                 if (eqn.charAt(i - 1) == '-')
  45.                 {
  46.                     String x = eqn.substring(i, i + 1);
  47.                     if (x != "+" && x != "-")
  48.                     {
  49.                         int n = Integer.parseInt(x, 10);
  50.                         coeffx[j++] = n;
  51.                     }
  52.                 } else
  53.                 {
  54.                     String x = eqn.substring(i, i + 1);
  55.                     if (x != "+" && x != "-")
  56.                     {
  57.                         int n = -(Integer.parseInt(x, 10));
  58.                         coeffx[j++] = n;
  59.                     }
  60.                 }
  61.                 i += 3;
  62.             }
  63.             if (eqn.charAt(i + 1) != 'x' && i < eqn.indexOf("="))
  64.             {
  65.                 if (eqn.charAt(i - 1) == '-')
  66.                 {
  67.                     String x = eqn.substring(i, i + 1);
  68.                     if (x != "+" && x != "-")
  69.                     {
  70.                         int n = -(Integer.parseInt(x, 10));
  71.                         constant[k++] = n;
  72.                     }
  73.                 } else
  74.                 {
  75.                     String x = eqn.substring(i, i + 1);
  76.                     if (x != "+" && x != "-")
  77.                     {
  78.                         int n = Integer.parseInt(x, 10);
  79.                         constant[k++] = n;
  80.                     }
  81.                 }
  82.                 i += 2;
  83.             }
  84.             if (eqn.charAt(i + 1) != 'x' && i > eqn.indexOf("="))
  85.             {
  86.                 if (eqn.charAt(i - 1) == '-')
  87.                 {
  88.                     String x = eqn.substring(i, i + 1);
  89.                     if (x != "+" && x != "-")
  90.                     {
  91.                         int n = Integer.parseInt(x, 10);
  92.                         constant[k++] = n;
  93.                     }
  94.                 } else
  95.                 {
  96.                     String x = eqn.substring(i, i + 1);
  97.                     if (x != "+" && x != "-")
  98.                     {
  99.                         int n = -(Integer.parseInt(x, 10));
  100.                         constant[k++] = n;
  101.                     }
  102.                 }
  103.                 i += 2;
  104.             }
  105.  
  106.         }
  107.         for (int i = 0; i < coeffx.length; i++)
  108.             coeffSum += coeffx[i];
  109.         for (int i = 0; i < constant.length; i++)
  110.             constSum += constant[i];
  111.         ans = constSum / coeffSum;
  112.         System.out.println("Value of x = " + (-ans));
  113.         in.close();
  114.     }
  115.  
  116. }

Output:

advertisement
$ javac LEquation.java
$ java LEquation
 
Enter a linear equation
 
2x+5=4x+9
Value of x = -2.0

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.