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!

## 3 Replies - 9864 Views - Last Post: 15 December 2008 - 05:15 PM

##
**Replies To:** Solve an second degree equation

### #2

## 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.

Give it a try and see what you come up with.

### #3

## 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."); }

### #4

## Re: Solve an second degree equation

Posted 15 December 2008 - 05:15 PM

Thanks your solution solved it.

Page 1 of 1