Advertisement
adriyanbulgary

Console Input-Output - Task 6

Jun 14th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. /*
  3.  * 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
  4.  */
  5. class QuadraticEquation
  6. {
  7.     static int Main()
  8.     {
  9.         Console.Write("a = ");
  10.         float a = float.Parse(Console.ReadLine());
  11.         Console.Write("b = ");
  12.         float b = float.Parse(Console.ReadLine());
  13.         Console.Write("c = ");
  14.         float c = float.Parse(Console.ReadLine());
  15.         double D = b * b - (4 * a * c);
  16.         double x1, x2;
  17.         if (D > 0)
  18.         {
  19.             x1 = (-b + Math.Sqrt(D)) / (2 * a);
  20.             x2 = (-b - Math.Sqrt(D)) / (2 * a);
  21.             Console.WriteLine("X1 = {0:0.####,10}\nX2 = {1:0.####,10}",x1,x2);
  22.             Console.Read();
  23.             return 1;
  24.         }
  25.         if (D < 0)
  26.         {
  27.             Console.WriteLine("no real roots");
  28.             Console.Read();
  29.             return 1;
  30.         }
  31.         else
  32.         {
  33.             x1 = -b / (2 * a);
  34.             Console.WriteLine("X1=X2={0:0.####,10}", x1);
  35.             Console.Read();
  36.         }
  37.         return 0;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement