This C# Program Finds Roots of a Quadratic Equation. Here A quadratic equation is a second-order polynomial equation expressed in a single variable, x, with a ≠ 0: ax2+bx+c=0 and has two roots which is found and displayed.

Here is source code of the C# Program to Find Roots of a Quadratic Equation. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

`/*`

`* C# Program to Find Roots of a Quadratic Equation`

`*/`

using System;

`namespace example`

`{`

`class Quadraticroots`

`{`

double a, b, c;

public void read()

`{`

Console.WriteLine(" \n To find the roots of a quadratic equation of the form a*x*x + b*x + c = 0");

Console.Write("\n Enter value for a : ");

a = double.Parse(Console.ReadLine());

Console.Write("\n Enter value for b : ");

b = double.Parse(Console.ReadLine());

Console.Write("\n Enter value for c : ");

c = double.Parse(Console.ReadLine());

`}`

public void compute()

`{`

int m;

double r1, r2, d1;

d1 = b * b - 4 * a * c;

if (a == 0)

m = 1;

else if (d1 > 0)

m = 2;

else if (d1 == 0)

m = 3;

`else`

m = 4;

switch (m)

`{`

case 1: Console.WriteLine("\n Not a Quadratic equation, Linear equation");

Console.ReadLine();

break;

case 2: Console.WriteLine("\n Roots are Real and Distinct");

r1 = (-b + Math.Sqrt(d1)) / (2 * a);

r2 = (-b - Math.Sqrt(d1)) / (2 * a);

Console.WriteLine("\n First root is {0:#.##}", r1);

Console.WriteLine("\n Second root is {0:#.##}", r2);

Console.ReadLine();

break;

case 3: Console.WriteLine("\n Roots are Real and Equal");

r1 = r2 = (-b) / (2 * a);

Console.WriteLine("\n First root is {0:#.##}", r1);

Console.WriteLine("\n Second root is {0:#.##}", r2);

Console.ReadLine();

break;

case 4: Console.WriteLine("\n Roots are Imaginary");

r1 = (-b) / (2 * a);

r2 = Math.Sqrt(-d1) / (2 * a);

Console.WriteLine("\n First root is {0:#.##} + i {1:#.##}", r1, r2);

Console.WriteLine("\n Second root is {0:#.##} - i {1:#.##}", r1, r2);

Console.ReadLine();

break;

`}`

`}`

`}`

`class Roots`

`{`

public static void Main()

`{`

Quadraticroots qr = new Quadraticroots();

qr.read();

qr.compute();

`}`

`}`

`}`

Here is the output of the C# Program:

To find the roots of a quadratic equation of the form a*x*x + b*x + c = 0 Enter value for a : 3.5 Enter value for b : 2.5 Enter value for c : 1.0 Roots are Imaginary First root is -.36 + i .4 Second root is -.36 - i .4

**Sanfoundry Global Education & Learning Series – 1000 C# Programs.**

If you wish to look at all C# Programming examples, go to 1000 C# Programs.