• [Java] Simple And Easy Way To Solve A Quadratic Equation

    ShikhaTan Member

    Hey guys this is a simple stub for solving a quadratic equation.

    while (true) {
             println ();
             println ("This program gives both solutions to a quadratic equation and its roots.");
             println (" Example: 5x^2 + 7x - 8 ");
             println ();
    
             //Prints the explanation of the program and an example of the format
    
             double A = readDouble("      How many x^2? (5 in the example)        ");
             double B = readDouble("      How many x? (7 in the example)          ");
             double C = readDouble("      How many units? (-8 in the example)     ");
             println ();
    
             //Requests user input
    
             double Totalpositive = (-B + (Math.sqrt ((B*B) - (4*A*C)))) / (2*A);
             double Totalnegative = (-B - (Math.sqrt ((B*B) - (4*A*C)))) / (2*A);
    
             //Solves for both solutions of the quadratic, using the quadratic formula
    
             println ("Solution one ---->  " + Totalpositive );
             println ("Solution two ---->  " + Totalnegative );
             println ();
             println ("Roots:  ");
             println ();
             println ("      [ X + " + -Totalpositive + " ; X + " + -Totalnegative + " ]");
    
             //Prints the solutions and the roots
          }
       }
    
Viewing 0 reply threads
  • You must be logged in to reply to this topic.