Advertisement
Guest User

Quadratic Equation

a guest
Mar 29th, 2015
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. // Problem 6. Quadratic Equation
  2.  
  3. /*Write a program that reads the coefficients a, b and c
  4.  * of a quadratic equation ax2 + bx + c = 0
  5.  * and solves it (prints its real roots).
  6.  
  7. Examples:
  8.  a      b       c   roots
  9.  2      5      -3   x1=-3; x2=0.5
  10. -1      3       0   x1=3; x2=0
  11. -0.5    4      -8   x1=x2=4
  12.  5      2       8   no real roots
  13.  */
  14.  
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Globalization;
  18. using System.Text;
  19. using System.Threading;
  20.  
  21. class QuadraticEquation
  22. {
  23.     // the quadratic equation is a second order of polynomial equation in a single variable
  24.     // x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
  25.  
  26.     #region SolveQuadratic(double a, double b, double c)
  27.  
  28.     public static void SolveQuadratic(double a, double b, double c)
  29.     {
  30.         //Calculating the discriminant of the square root
  31.         double discriminant = (b * b) - 4 * a * c;
  32.         if (discriminant < 0)
  33.         {
  34.             // no real roots
  35.             Console.WriteLine("\nThere are no real roots\n");
  36.         }
  37.         else
  38.         {
  39.             // if x1 == x2 : one real root
  40.             double discriminantRoot = Math.Sqrt(discriminant);
  41.             double x1 = (-b + discriminantRoot) / (2 * a);
  42.             double x2 = (-b - discriminantRoot) / (2 * a);
  43.             Console.WriteLine(x1 == x2 ? "\nOne real root {0}\n" : "\nTwo real roots: {0} and {1}\n", x1, x2);
  44.         }
  45.     }
  46.     #endregion
  47.  
  48.     static void Main()
  49.     {
  50.         // allows me to instruct the console to read the dot as decimal point in floating-point type numbers
  51.         CultureInfo en = new CultureInfo("en-US");
  52.         Thread.CurrentThread.CurrentCulture = en;
  53.  
  54.         string intro = @"The quadratic equation is a second order of polynomial equation
  55. in a single variable: x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
  56. Please, enter the values for a, b, and c.";
  57.         Console.WriteLine(intro);
  58.  
  59.         string input;
  60.         double number;
  61.  
  62.         double[] numbers = new double[3];
  63.  
  64.         // limits the number entries to 3, and numbers the input entries
  65.         for (int i = 0; i < 3; i++)
  66.         {
  67.             do // validate input for number
  68.             {
  69.                 Console.Write("\nPlease, enter value {0}: ", i + 1);
  70.                 input = Console.ReadLine().Replace(",", ".");
  71.  
  72.             } while (!double.TryParse(input, out number));
  73.  
  74.             numbers[i] = number; // adds the entered numbers into the numbers list
  75.         }
  76.  
  77.         // calling SolveQuadratic method
  78.         SolveQuadratic(numbers[0], numbers[1], numbers[2]);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement