c-sharp
  1. c-sharp-program-to-find-the-roots-of-a-quadratic-equation

Program to Find the Roots of a Quadratic Equation - (C# Basic Programs)

Finding the roots of a quadratic equation is a commonly asked problem in mathematics and computer science. In this tutorial, we will discuss a C# program to find the roots of a quadratic equation.

Syntax

double root1, root2;
double d = b * b - 4 * a * c;  // discriminant
if (d > 0) // real and different roots
{
   root1 = (-b + Math.Sqrt(d)) / (2 * a);
   root2 = (-b - Math.Sqrt(d)) / (2 * a);
   Console.WriteLine("Roots are real and different");
   Console.WriteLine("Root1 = {0}, Root2 = {1}", root1, root2);
}
else if (d == 0) // real and equal roots
{
   root1 = -b / (2 * a);
   root2 = root1;
   Console.WriteLine("Roots are real and same");
   Console.WriteLine("Root1 = {0}, Root2 = {1}", root1, root2);
}
else // complex roots
{
   double realPart = -b / (2 * a);
   double imaginaryPart = Math.Sqrt(-d) / (2 * a);
   Console.WriteLine("Roots are complex and different");
   Console.WriteLine("Root1 = {0} + {1}i", realPart, imaginaryPart);
   Console.WriteLine("Root2 = {0} - {1}i", realPart, imaginaryPart);
}

Example

using System;

namespace RootsOfQuadraticEquation
{
    class Program
    {
        static void Main(string[] args)
        {
            double a, b, c, root1, root2;
            Console.WriteLine("Enter coefficients a, b and c: ");
            a = double.Parse(Console.ReadLine());
            b = double.Parse(Console.ReadLine());
            c = double.Parse(Console.ReadLine());
            double d = b * b - 4 * a * c;
            if (d > 0) // Real and different roots
            {
                root1 = (-b + Math.Sqrt(d)) / (2 * a);
                root2 = (-b - Math.Sqrt(d)) / (2 * a);
                Console.WriteLine("Roots are real and different");
                Console.WriteLine("Root1 = {0}, Root2 = {1}", root1, root2);
            }
            else if (d == 0) // Real and same roots
            {
                root1 = -b / (2 * a);
                root2 = root1;
                Console.WriteLine("Roots are real and same");
                Console.WriteLine("Root1 = {0}, Root2 = {1}", root1, root2);
            }
            else // Complex roots
            {
                double realPart = -b / (2 * a);
                double imaginaryPart = Math.Sqrt(-d) / (2 * a);
                Console.WriteLine("Roots are complex and different");
                Console.WriteLine("Root1 = {0} + {1}i", realPart, imaginaryPart);
                Console.WriteLine("Root2 = {0} - {1}i", realPart, imaginaryPart);
            }
            Console.ReadLine();
        }
    }
}

Output:

Enter coefficients a, b and c:
2
4
-6
Roots are real and different
Root1 = 0.732050807568877, Root2 = -2.73205080756888

Explanation

The above C# program first takes input three coefficients of the quadratic equation a, b, and c. It then calculates the discriminant using the formula d = b * b - 4 * a * c. It then checks the value of the discriminant to determine the nature of the roots. If the discriminant is positive, the roots are real and different. If the discriminant is zero, the roots are real and same. Otherwise, the roots are complex and different.

Use

The C# program to find the roots of a quadratic equation is a basic program that is useful for learning the C# programming language and basic concepts like conditional statements and mathematical calculations. It can also be used in various scientific and mathematical applications where finding the roots of a quadratic equation is required.

Summary

In this tutorial, we have discussed a C# program to find the roots of a quadratic equation. We have covered the syntax, example, explanation, use, and importance of this program. By using this program, one can learn the C# syntax and basic concepts like conditional statements and mathematical calculations for solving real-world problems.

Published on: