The Program in C# Program to calculate Root of Quadratic Equation is odd or even given below:
using System;
namespace QuadraticEquation
{
class Program
{
static void Main(string[] args)
{
double a, b, c;
double root1, root2, imaginaryPart, realPart, discriminant;
Console.WriteLine("Hello Codeauri Family, enter the coefficients of the quadratic equation (a, b, c):");
a = Convert.ToDouble(Console.ReadLine());
b = Convert.ToDouble(Console.ReadLine());
c = Convert.ToDouble(Console.ReadLine());
// Calculate the discriminant
discriminant = b * b - 4 * a * c;
// Check if the roots are real or imaginary
if (discriminant > 0)
{
root1 = (-b + Math.Sqrt(discriminant)) / (2 * a);
root2 = (-b - Math.Sqrt(discriminant)) / (2 * a);
Console.WriteLine("The roots of the quadratic equation are real and different.");
Console.WriteLine("Root 1: " + root1);
Console.WriteLine("Root 2: " + root2);
}
else if (discriminant == 0)
{
root1 = root2 = -b / (2 * a);
Console.WriteLine("The roots of the quadratic equation are real and equal.");
Console.WriteLine("Root: " + root1);
}
else
{
realPart = -b / (2 * a);
imaginaryPart = Math.Sqrt(-discriminant) / (2 * a);
Console.WriteLine("Okay, the roots of the quadratic equation are complex and different.");
Console.WriteLine("Root 1: " + realPart + " + " + imaginaryPart + "i");
Console.WriteLine("Root 2: " + realPart + " - " + imaginaryPart + "i");
}
}
}
}
Output:
Hello Codeauri Family, enter the coefficients of the quadratic equation (a, b, c):
1
8
4
The roots of the quadratic equation are real and different.
Root 1: -0.535898384862246
Root 2: -7.46410161513775
Pro-Tips💡
Here are the step by step execution of above program:
- A namespace named “QuadraticEquation” is declared.
- A class named “Program” is declared inside the namespace.
- The Main() method is defined inside the Program class. This is the entry point of the program.
- Three variables, a, b, and c, are declared to store the coefficients of the quadratic equation.
- The user is prompted to enter the coefficients of the quadratic equation through the console. The values entered by the user are stored in the variables a, b, and c, using the Console.ReadLine() method.
- The discriminant of the equation is calculated using the formula: b^2 – 4ac.
- An if-else statement is used to check the value of the discriminant. a. If the discriminant is greater than 0, the roots of the equation are real and different. The roots are calculated using the formula (-b + sqrt(discriminant)) / (2 * a) and (-b – sqrt(discriminant)) / (2 * a). The roots are then printed to the console.
b. If the discriminant is equal to 0, the roots of the equation are real and equal. The root is calculated using the formula -b / (2 * a) and printed to the console.
c. If the discriminant is less than 0, the roots are complex and different. The real part of the root is calculated using the formula -b / (2 * a) and the imaginary part of the root is calculated using the formula sqrt(-discriminant) / (2 * a). The roots are then printed to the console.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.