Uncategorized

C# Program to calculate Root of Quadratic Equation

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:

  1. A namespace named “QuadraticEquation” is declared.
  2. A class named “Program” is declared inside the namespace.
  3. The Main() method is defined inside the Program class. This is the entry point of the program.
  4. Three variables, a, b, and c, are declared to store the coefficients of the quadratic equation.
  5. 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.
  6. The discriminant of the equation is calculated using the formula: b^2 – 4ac.
  7. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

WAP in C++ to Calculate the Volume of a Cone

The Program in C++ to Calculate the Volume of a Cone is given below: Output: Hello Codeauri Family, Please Input the Cone’s radius here: 7Similarly, input the Cone’s height…

WAP in C++ to Print Mystery Series from 1 to 50

The Program in C++ to Print Mystery Series from 1 to 50 is given below: Output: Mystery series number 1Mystery series number 2Mystery series number 3Mystery series number 4Mystery…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X