Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class QuadraticEquationRoots
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter a quadratic equation of this type: ax^2 + bx + c = 0.");
- string equation = Console.ReadLine();
- int cA = int.Parse(equation.Substring(0, equation.IndexOf("x^2")).Replace(" ", ""));
- equation = equation.Substring(equation.IndexOf("x^2") + 3);
- int cB = int.Parse(equation.Substring(0, equation.IndexOf("x")).Replace(" ", ""));
- equation = equation.Substring(equation.IndexOf("x") + 1);
- int cC = int.Parse(equation.Substring(0, equation.IndexOf("=")).Replace(" ", ""));
- Console.WriteLine("a = " + cA);
- Console.WriteLine("b = " + cB);
- Console.WriteLine("c = " + cC);
- double D = (cB * cB) - (4 * cA * cB);
- Console.WriteLine("Discriminant: " + D);
- double b1 = 0;
- double b2 = 0;
- if (D == 0)
- {
- double result1 = b1 = b2 = ((-cB) / (2 * cA));
- Console.WriteLine("b1 = b2 = " + result1);
- }
- else if (D > 0)
- {
- double result2 = b1 = ((-cB) + Math.Sqrt(D)) / (2 * cA);
- double result3 = b2 = ((-cB) - Math.Sqrt(D)) / (2 * cA);
- Console.WriteLine("b1 = " + result2);
- Console.WriteLine("b2 = " + result3);
- }
- else
- {
- Console.WriteLine("Equation has no solution");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment