Solve an second degree equation

is it any good solution for this?

Page 1 of 1

3 Replies - 6178 Views - Last Post: 15 December 2008 - 05:15 PM Rate Topic: -----

#1 micke  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-August 08

Solve an second degree equation

Post icon  Posted 14 December 2008 - 03:34 PM

Hi,

is there any good solution for solving an second degree equation like this : ax2 + bx + c = 0 i was thinking of Math(); but diden't find anything!

Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: Solve an second degree equation

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Solve an second degree equation

Posted 14 December 2008 - 03:50 PM

The solution is not as simple as calling a math function. But there is a pretty simple formula, Quadratic Formula, that describes how to easily solve these polynomials.

Give it a try and see what you come up with.
Was This Post Helpful? 0
  • +
  • -

#3 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 773
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Solve an second degree equation

Posted 14 December 2008 - 03:52 PM

There is no Math() function that will solve the quadratic (second degree) equation. However, this can be easily accomplished if you know how to find the solutions for the equation on paper, as you use the same formulas. The C# code will look like this:

// Declare three variables that will contain the coefficients.
            int a, b, c = 0;

            // Declare three double-type variables that will contain the intermediate value (d) and two solutions (x1 and x2).
            double d, x1, x2 = 0;

            // Set your coefficients here. Change this code for user input if you want the user to
            // set the coefficients.
            a = 1;
            b = -121;
            c = -21;

            // Calculate the intermediate value.
            d = b * b - 4 * a * c;

            // Verify the intermediate value.

            // If the value is bigger than zero, there are two different solutions.
            if (d > 0)
            {
                // Calculate the solutions and display them.
                x1 = (-b - Math.Sqrt(d)) / 4 * a;
                x2 = (-b + Math.Sqrt(d)) / 4 * a;
                MessageBox.Show("The soultions are " + x1.ToString() + " and " + x2.ToString() + ".");
            }
            // If the intermediate value is 0, both solutions (x1 and x2) are equal.
            else if (d == 0)
            {
                // Calculate the solution and display it.
                x1 = (-b - Math.Sqrt(d)) / 4 * a;
                MessageBox.Show("The soultions are the both the same: {0}.", x1.ToString());
            }
            // For any other value of the intermediate value (less than 0) there are no solutions.
            else
            {
                MessageBox.Show("There are no solutions for this equation.");
            }


Was This Post Helpful? 1
  • +
  • -

#4 micke  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-August 08

Re: Solve an second degree equation

Posted 15 December 2008 - 05:15 PM

Thanks your solution solved it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1