Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *Write a program that enters the coefficients a, b and c of a quadratic equation
- a*x2 + b*x + c = 0
- and calculates and prints its real roots. Note that quadratic equations may have 0, 1 or 2 real roots.
- */
- using System;
- class CalculateQuadraticEquation
- {
- static void Main()
- {
- double coefficientA, coefficientB, coefficientC, discriminantD, DRoot2, resultX1, resultX2;
- string invalidInput = "Invalid input! Please enter value between " + double.MinValue + " and " + double.MaxValue + "!" + Environment.NewLine; ;
- Console.WriteLine("ax^2 + bx + c = 0, a != 0");
- Console.WriteLine("Enter value of coefficient a: a = ");
- while (!(double.TryParse(Console.ReadLine(), out coefficientA) && coefficientA >= double.MinValue && coefficientA <= double.MaxValue))
- {
- Console.WriteLine(invalidInput);
- Console.WriteLine("Enter value of coefficient a: a = ");
- }
- Console.WriteLine("Enter value of coefficient b: b = ");
- while (!(double.TryParse(Console.ReadLine(), out coefficientB) && coefficientB >= double.MinValue && coefficientB <= double.MaxValue))
- {
- Console.WriteLine(invalidInput);
- Console.WriteLine("Enter value of coefficient b: b = ");
- }
- Console.WriteLine("Enter value of coefficient c: c = ");
- while (!(double.TryParse(Console.ReadLine(), out coefficientC) && coefficientC >= double.MinValue && coefficientC <= double.MaxValue))
- {
- Console.WriteLine(invalidInput);
- Console.WriteLine("Enter value of coefficient c: c = ");
- }
- if (coefficientA != 0)
- {
- discriminantD = (Math.Pow(coefficientB, 2)) - (4 * coefficientA * coefficientC);
- if (discriminantD < 0)
- {
- Console.WriteLine("The quadratic equation {0}x^2 + {1}x + {2} = 0 has no real roots because discriminant D = {3}" + Environment.NewLine, coefficientA, coefficientB, coefficientC, discriminantD);
- }
- if (discriminantD > 0)
- {
- DRoot2 = Math.Sqrt(discriminantD);
- resultX1 = (((-1) * coefficientB) + DRoot2) / (2 * coefficientA);
- resultX2 = (((-1) * coefficientB) - DRoot2) / (2 * coefficientA);
- Console.WriteLine("The quadratic equation {0}x^2 + {1}x + {2} = 0 has two real roots: x1 = {3} and x2 = {4} and discriminant D = {5} !" + Environment.NewLine, coefficientA, coefficientB, coefficientC, resultX1, resultX2, discriminantD);
- }
- if (discriminantD == 0)
- {
- resultX1 = (((-1) * coefficientB)) / (2 * coefficientA);
- Console.WriteLine("The quadratic equation {0}x^2 + {1}x + {2} = 0 has one real root: x1 = {3} and discriminant D = {4} !" + Environment.NewLine, coefficientA, coefficientB, coefficientC, resultX1, discriminantD);
- }
- }
- else
- {
- if (coefficientB != 0)
- {
- resultX1 = (-1) * coefficientC / coefficientB;
- Console.WriteLine("{0}x + {1} = 0 is not quadratic equation!" + Environment.NewLine + "After all: x = {2}" + Environment.NewLine, coefficientB, coefficientC, resultX1);
- }
- else
- {
- if (coefficientC != 0)
- {
- Console.WriteLine("Can't divide by 0! The equation has no solution!" + Environment.NewLine);
- }
- else
- {
- Console.WriteLine("The equation has +- infinity real roots!" + Environment.NewLine);
- }
- }
- }
- Main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment