Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- * 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
- */
- class QuadraticEquation
- {
- static int Main()
- {
- Console.Write("a = ");
- float a = float.Parse(Console.ReadLine());
- Console.Write("b = ");
- float b = float.Parse(Console.ReadLine());
- Console.Write("c = ");
- float c = float.Parse(Console.ReadLine());
- double D = b * b - (4 * a * c);
- double x1, x2;
- if (D > 0)
- {
- x1 = (-b + Math.Sqrt(D)) / (2 * a);
- x2 = (-b - Math.Sqrt(D)) / (2 * a);
- Console.WriteLine("X1 = {0:0.####,10}\nX2 = {1:0.####,10}",x1,x2);
- Console.Read();
- return 1;
- }
- if (D < 0)
- {
- Console.WriteLine("no real roots");
- Console.Read();
- return 1;
- }
- else
- {
- x1 = -b / (2 * a);
- Console.WriteLine("X1=X2={0:0.####,10}", x1);
- Console.Read();
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement