## 3 Replies - 4450 Views - Last Post: 26 February 2012 - 01:49 AM

### #1 PixelCard Reputation: 48
• Posts: 139
• Joined: 20-June 08

Posted 19 July 2008 - 10:37 AM

Description: This snippet was created and tested in a standard C# console application, so it will work in any other type of C# applications (WinForms, WPF) with some minor modifications.This code snippet solves a quadratic equation.
```using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// a, b and c are the coefficients of the equation.
int a, b, c;
// d is the discriminant.
int d;
// x1 and x2 are the solutions.
double x1, x2;

Console.WriteLine("The equation structure: axx+bx+c=0");

// Get the value of a
entA:Console.Write("Enter a:");
try{
// Convert the input to integer.
}
catch{
// In case of invalid input, show an error message and return
// to input.
Console.WriteLine("Invalid Data!");
goto entA;
}
// Get the value of b
entB:Console.Write("Enter b:");
try{
// Convert the input to integer.
}
catch{
// In case of invalid input, show an error message and return
// to input.
Console.WriteLine("Invalid Data!");
goto entB;
}
// Get the value of c
entC:Console.Write("Enter c:");
try{
// Convert the input to integer.
}
catch{
// In case of invalid input, show an error message and return
// to input.
Console.WriteLine("Invalid Data!");
goto entC;
}

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

// Verify the discriminant.
if (d == 0) // If the discriminant is 0, both solutions are equal.
{
x1 = x2 = -b / (2 * a);
Console.WriteLine("Both solutions are {0}.", x1);
}
else if (d < 0) // If the discriminant is negative, there are no solutions.
{
Console.WriteLine("No solutions for the equation!");
}
else // In other cases the discriminant is positive, so there are two different solutions.
{
x1 = (-b - Math.Sqrt(d) )/ (2 * a);
x2 = (-b + Math.Sqrt(d)) / (2 * a);
Console.WriteLine("The solutions are {0} and {1}.", x1, x2);
}
}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Solve a quadratic equation

### #2 eclipsed4utoo Reputation: 1536
• Posts: 5,972
• Joined: 21-March 08

## Re: Solve a quadratic equation

Posted 05 December 2008 - 06:36 AM

maybe the code could be modified to not use labels and "goto" statements.

## Re: Solve a quadratic equation

Posted 01 July 2011 - 02:49 PM

I created a similar program: http://www.youtube.c...nel_video_title

## Re: Solve a quadratic equation

Posted 26 February 2012 - 01:49 AM

ok but when it finish calculation the console turn of.How to make it to prepare for next calculation

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }