Advertisement
Guest User

Untitled

a guest
Jan 11th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System;
  2.  
  3. class QuadraticEquation
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("Enter 'a': ");
  8.         double a = double.Parse(Console.ReadLine());
  9.         Console.Write("Enter 'b': ");
  10.         double b = double.Parse(Console.ReadLine());
  11.         Console.Write("Enter 'c': ");
  12.         double c = double.Parse(Console.ReadLine());
  13.  
  14.         double discriminant = Math.Pow(b, 2) - (4 * a * c);
  15.        
  16.         double x1;
  17.         double x2;
  18.  
  19.         if (a == 0)
  20.         {
  21.             Console.WriteLine("'a' must not be '0'");
  22.         }
  23.  
  24.         else if (discriminant > 0 && a != 0)
  25.         {
  26.             x1 = ((-b) - (Math.Sqrt(discriminant))) / (2 * a);
  27.             x2 = ((-b) + (Math.Sqrt(discriminant))) / (2 * a);
  28.             Console.WriteLine("x1 = {0}; x2 = {1}", x1, x2);
  29.         }
  30.  
  31.         else if (discriminant == 0 && a != 0)
  32.         {
  33.             x1 = x2 = -(b / (2 * a));
  34.             Console.WriteLine("x1 = x2 = {0}", x1);
  35.         }
  36.  
  37.         else
  38.         {
  39.             Console.WriteLine("no real roots");
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement