Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class QuadraticEquationRoots
- {
- static void Main()
- {
- //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.
- Console.Write("Please enter the coefficient number a:");
- double a = double.Parse(Console.ReadLine()); // quadratic coeficient a.
- Console.Write("Please enter the coefficient number b:");
- double b = double.Parse(Console.ReadLine()); // linear coeficient b
- Console.Write("Please enter the constant term c:");
- double c = double.Parse(Console.ReadLine()); // free term
- double delta = b * b - 4 * a * c;
- double x1, x2;
- if (a == 0 && b == 0 && c == 0)
- {
- Console.WriteLine("The possible roots are infinite!");
- }
- else if (a == 0 && b == 0 && c != 0)
- {
- Console.WriteLine("There are no real roots!");
- }
- else if (a == 0 && b != 0)
- {
- Console.WriteLine("Your equation is linear and has root x = {0}", -(c / b));
- }
- else if (delta < 0)
- {
- Console.WriteLine("The quadratic equation has no real roots.");
- }
- else
- {
- x1 = ((-b + Math.Sqrt(delta))) / (2 * a);
- x2 = ((-b - Math.Sqrt(delta))) / (2 * a);
- Console.WriteLine("The equation has roots x1={0} and x2={1}", x1, x2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement