# Solve an second degree equation

Page 1 of 1

## 3 Replies - 10168 Views - Last Post: 15 December 2008 - 05:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=76709&amp;s=b622fd2d4783c3fa3ec2a6d62e69f7c4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 micke

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

# Solve an second degree equation

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

• Student of Life

Reputation: 421
• 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.

### #3 Core

• using System.Linq;

Reputation: 782
• Posts: 5,101
• 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.");
}

```

### #4 micke

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

## Re: Solve an second degree equation

Posted 15 December 2008 - 05:15 PM