pupesko

SoftUni - 4.6* Quadratic Equation

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