Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program that reads the coefficients a, b and c of a quadratic equation ax2 + bx + c = 0 and solves it (prints its real roots).
- using System;
- class QuadraticEq
- {
- static void Main()
- {
- Console.Write("Enter a= ");
- double a = double.Parse(Console.ReadLine());
- Console.Write("Enter b= ");
- double b = double.Parse(Console.ReadLine());
- Console.Write("Enter c= ");
- double c = double.Parse(Console.ReadLine());
- double D = (b * b) - (4 * a * c);
- if (D < 0)
- {
- Console.WriteLine("no real roots");
- }
- else if (D == 0)
- {
- double x = -b / (2 * a);
- Console.WriteLine("x1 = x2 = " + x);
- }
- else
- {
- double x1 = (-b - Math.Sqrt(D))/ (2 * a);
- Console.WriteLine("x1 = " + x1);
- double x2 = (-b + Math.Sqrt(D))/ (2 * a);
- Console.WriteLine("x2 = " + x2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment