Advertisement
MladenMladenov

Quadratic Equation Solution NEW

Mar 29th, 2015
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2.  
  3. class QuadraticEquation
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Please enter \"a\":");  // takes the value of the  coefficients
  8.         double a = double.Parse(Console.ReadLine());
  9.         Console.WriteLine("Please enter \"b\":");
  10.         double b = double.Parse(Console.ReadLine());
  11.         Console.WriteLine("Please enter\"c\":");
  12.         double c = double.Parse(Console.ReadLine());
  13.         double D = Math.Pow(b, 2) - 4 * a * c;  // Formula for D
  14.         if (D < 0) // gives you result if D is smaller than 0
  15.         {
  16.             Console.WriteLine("No real roots");
  17.            
  18.         }
  19.         else if (D == 0) // gives you result if D is equal to 0
  20.         {
  21.             double x3 = (-b) / (2 * a);
  22.             Console.WriteLine("x1=x2= {0}", x3);
  23.            
  24.         }
  25.         else
  26.         {
  27.             double x1 = ((-b) - Math.Sqrt(D)) / (2 * a); // for all other cases this will be your equasion
  28.             Console.WriteLine("Root x1 is:{0}", x1);
  29.             double x2 = ((-b) + Math.Sqrt(D)) / (2 * a);
  30.             Console.WriteLine("Root x2 is:{0}", x2);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement