Created
July 24, 2015 12:45
-
-
Save MladenMladenov/5594eeede84385e2ed41 to your computer and use it in GitHub Desktop.
C# Quadratic-Equation-Formula
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class QuadraticEquation | |
{ | |
static void Main() | |
{ | |
Console.WriteLine("Please enter \"a\":"); // takes the value of the coefficients | |
double a = double.Parse(Console.ReadLine()); | |
Console.WriteLine("Please enter \"b\":"); | |
double b = double.Parse(Console.ReadLine()); | |
Console.WriteLine("Please enter\"c\":"); | |
double c = double.Parse(Console.ReadLine()); | |
double D = Math.Pow(b, 2) - 4 * a * c; // Formula for D | |
if (D < 0) // gives you result if D is smaller than 0 | |
{ | |
Console.WriteLine("No real roots"); | |
return; | |
} | |
if(D == 0) // gives you result if D is equal to 0 | |
{ | |
double x3 = (-b) / (2 * a); | |
Console.WriteLine("x1=x2= {0}", x3); | |
return; | |
} | |
double x1 = ((-b) - Math.Sqrt(D)) / (2 * a); // for all other cases this will be your equasion | |
Console.WriteLine("Root x1 is:{0}", x1); | |
double x2 = ((-b) + Math.Sqrt(D)) / (2 * a); | |
Console.WriteLine("Root x2 is:{0}", x2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wow thanks