-3

Could you tell me what's wrong with my code for solving a quadratic equation? Don't be cringed out by my work, because I'm still very new to the program.

class Program
{
    static void Main(string[] args)
    {
        double a, b, c, x1, x2, x, D;
        String A;
        String B;
        String C;
        Console.Write("a=");
        A = Console.ReadLine(); 
        Console.Write("b=");
        B = Console.ReadLine();
        Console.Write("c=");
        C = Console.ReadLine();
        a = Convert.ToDouble(A);
        b = Convert.ToDouble(B);
        c = Convert.ToDouble(C);
        D =(b * b - 4 * a * c);
        if (D > 0)
        {
            x1 = (-b + Math.Sqrt(D)) / (2 * a);
            x2 = (-b - Math.Sqrt(D)) / (2 * a);
            Console.WriteLine("x1=" + x1);
            Console.WriteLine("x2=" + x2);
        }
        else
            if (D < 0)
            {
                D = -D;
                x1 = (-b + Math.Sqrt(D)) / (2 * a);
                x2 = (-b - Math.Sqrt(D)) / (2 * a);
            }
            else
            {
                x = (-b / (2 * a));
                Console.WriteLine("x=" + x);
            }
        Console.ReadKey();

    }
}

It doesn't display any values when it is run.

strongbutgood
  • 655
  • 7
  • 22

2 Answers2

1

Try something like

class Program
{
    static void Main(string[] args)
    {
        double a, b, c, x1, x2, x, D;
        String A;
        String B;
        String C;
        Console.Write("a=");
        A = Console.ReadLine();
        Console.Write("b=");
        B = Console.ReadLine();
        Console.Write("c=");
        C = Console.ReadLine();
        a = Convert.ToDouble(A);
        b = Convert.ToDouble(B);
        c = Convert.ToDouble(C);

        if (a != 0)
        {
            D = (b * b - 4 * a * c);

            if (D > 0)
            {
                x1 = (-b + Math.Sqrt(D)) / (2 * a);
                x2 = (-b - Math.Sqrt(D)) / (2 * a);
                Console.WriteLine("x1=" + x1);
                Console.WriteLine("x2=" + x2);
            }
            else
            {
                if (D < 0)
                {
                    D = -D;
                    //x1 = (-b + Math.Sqrt(D)) / (2 * a);
                    //x2 = (-b - Math.Sqrt(D)) / (2 * a);

                    string complexX1 = ((-b) / (2 * a)).ToString() + "+" + (Math.Sqrt(D) / 2 * a).ToString() + "i";
                    string complexX2 = ((-b) / (2 * a)).ToString() + "-" + (Math.Sqrt(D) / 2 * a).ToString() + "i";

                    Console.WriteLine("complex x1=" + complexX1);
                    Console.WriteLine("complex x2=" + complexX2);
                }
                else
                {
                    x = (-b / (2 * a));
                    Console.WriteLine("x=" + x);
                }
            }
        }
        else
        {
            if (b != 0)
            {
                x = -c / b;

                Console.WriteLine("Grade 1 equation");
                Console.WriteLine("x=" + x);
            }
            else
            {
                Console.WriteLine("No equation.");
            }
        }

        Console.ReadKey();
    }
}

It seems that you were not writing to console in all the cases.

And also the way how the complex roots were treated was not entirely correct since the "i" was missing so that the real and imaginary part of the number were missing. Now the result should be fine also for complex roots.

I updated the code so now also cases when a is 0 (the equation is not quadric) and b is 0 (there is no equation) are handle.

Clock
  • 974
  • 3
  • 17
  • 35
1

Do not cram everything into a single Main method, decompose your implementation! In your very case, let's extract a method:

   // IEnumerable<Double>: quadratic equation can have 0..2 distinct real roots  
   private static IEnumerable<Double> SolveQuadratic(double a, double b, double c) {
     double D = b * b - 4 * a * c;

     if (D < 0)
       yield break; // No real solutions (they are both complex ones)
     else if (D == 0)
       yield return -b / (2 * a); // One distinct root    
     else {
       // Two distinct solutions
       yield return (-b - Math.Sqrt(D)) / (2 * a); 
       yield return (-b + Math.Sqrt(D)) / (2 * a); 
     }
   }

Then use it:

   ...
   a = Convert.ToDouble(A);
   b = Convert.ToDouble(B);
   c = Convert.ToDouble(C);

   var result = SolveQuadratic(a, b, c)
     .Select((x, i) => $"x{i + 1} = {x}");  

   Console.Write(String.Join(Environment.NewLine, result));

   Console.ReadKey();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215