# C# Program to Find the Roots of a Quadratic Equation

«
»

This is a C# Program to find roots of a quadratic equation.

Problem Description

This C# Program Finds Roots of a Quadratic Equation.

Problem Solution

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 are found and displayed.

Program/Source Code

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
{
{
double a, b, c;

{
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 : ");
Console.Write("\n Enter value for b : ");
Console.Write("\n Enter value for c : ");
}
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");
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);
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);
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);
break;
}
}
}

class Roots
{
public static void Main()
{
qr.compute();
}
}
}```
Program Explanation

In this C# program, we are reading three integer values using ‘a’, ’b’ and ‘c’ variables respectively. If else condition statement is used to check the entered value is equal to 0. If the condition is true execute the statement and print the statement as it is not a quadratic equation it and display as Error: Roots cannot be determined.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Otherwise if the condition is false, then execute the else statement. First find the discriminant using the formula:
disc = b * b – 4 * a * c.

Nested if else condition statement is used to display the 3 types of roots they are complex, distinct & equal roots from the equation.

If the equation value in ‘disc’ variable is less than 0 then it is imaginary root. Compute the real part and imaginary part using the formula
Real Part = -b /(2.0* a)
Imaginary Part = sqrt(abs(disc))/(2.0* a)

Otherwise, if the condition is false, then execute the elseif condition statement. Check the value of ‘disc’ variable is equal to 0. If the condition is true then roots are real and equal. Compute the real part and imaginary part using the formula
Real Part = -b / (2.0 * a)
Imaginary Part = Real Part

Otherwise, if both the condition are false, then execute another else if condition statement. Check the value of ‘disc’ variable is greater than 0. If the condition is true, then the roots are real and distinct. Execute the statement; compute the root1 and root2 variable values using the formula
Root1 = (-b + sqrt (disc) ) / (2.0 * a)
Root2 = (-b – sqrt (disc) ) / (2.0 * a)

Runtime Test Cases
```
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. 