Advertisement
VyaraG

QuadraticEquation

Nov 28th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 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 (prdoubles its real roots).
  4.  
  5. class QuadraticEquation
  6. {
  7.     static void Main()
  8.     {
  9.         Console.Write("Enter value for 'a': ");
  10.         double a = double.Parse(Console.ReadLine());
  11.         Console.Write("Enter value for 'b': ");
  12.         double b = double.Parse(Console.ReadLine());
  13.         Console.Write("Enter value for 'c': ");
  14.         double c = double.Parse(Console.ReadLine());
  15.         double D = (b * b) - (4 * a * c);
  16.         if (D < 0)
  17.         {
  18.             Console.WriteLine("no real roots");
  19.         }
  20.         else
  21.         {
  22.             if (D == 0)
  23.             {
  24.                 double x = -b /( 2 * a);
  25.                 Console.WriteLine("x1=x2= {0}", x);
  26.             }
  27.             else
  28.             {
  29.                 double x1 = ((-b) + Math.Sqrt(D)) / (2 * a);
  30.                 double x2 = ((-b) - Math.Sqrt(D)) / (2 * a);
  31.                 Console.WriteLine(" x1={0}; x2={1}", x1, x2);
  32.             }
  33.         }
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement