Note that there are some explanatory texts on larger screens.

plurals
  1. POImaginary numbers in C#
    primarykey
    data
    text
    <p>I'm trying to make a simple console-based program that can solve quadratic equations. I still haven't figured out how to calculate the answers if the discriminant is a negative number, so I just displayed a message as a placeholder. So can anybody explain how to do this? I'll post the whole program for any suggestions. Thanks.</p> <p>Properly working code (Thanks, Gunther Fox):</p> <pre><code>if(discriminant &lt; 0) { double x1 = -b/(2*a); discriminant = -discriminant ; double x2 = Math.Sqrt(discriminant)/(2*a); double x3 = -Math.Sqrt(discriminant)/(2*a); Console.WriteLine(x1.ToString() + " + " + x2.ToString() + " * i "); Console.WriteLine(x1.ToString() + " + " + x3.ToString() + " * i "); Console.ReadLine(); } </code></pre> <p>Old code:</p> <pre><code>class Program { static void Main(string[] args) { int choice; double ans1; double ans2; Console.WriteLine("~~~~~~~~ TERMINAL CALCULATOR ~~~~~~~~"); Console.WriteLine("1. Standard Calculator\n2. Quadratic Equation Solver\n3. Simple Interest Calculator"); choice = int.Parse(Console.ReadLine()); try { if (choice == 2) { Console.Write("Enter a value for 'a': "); double a = double.Parse(Console.ReadLine()); Console.Write("Enter a value for 'b': "); double b = double.Parse(Console.ReadLine()); Console.Write("Enter a value for 'c': "); double c = double.Parse(Console.ReadLine()); //Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a //Solve disriminant: (b*b) - (4*a*c) double discriminant = (b * b) - (4 * a * c); if (discriminant &lt; 0) { Console.WriteLine("No real solutions"); Console.ReadLine(); } else { double sqrt = Math.Sqrt(discriminant); ans1 = (-b + sqrt) / (2 * a); ans2 = (-b - sqrt) / (2 * a); Console.WriteLine("{" + ans1 + " , " + ans2 + "}"); Console.ReadLine(); } } if (choice == 1) { Console.Write("Enter first number: "); double num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number (Enter 0 if you want to use sqrt): "); double num2 = double.Parse(Console.ReadLine()); //Prompt user to choose an operation Console.WriteLine("Choose a math operator:\n1. +\n2. -\n3. x\n4. /\n5. ^\n6. Square root"); int mathOpr = int.Parse(Console.ReadLine()); if (mathOpr == 1) { double answer = num1 + num2; Console.WriteLine("\n\n" + answer); Console.ReadLine(); } if (mathOpr == 2) { double answer = num1 - num2; Console.WriteLine("\n\n" + answer); Console.ReadLine(); } if (mathOpr == 3) { double answer = num1 * num2; Console.WriteLine("\n\n" + answer); Console.ReadLine(); } if (mathOpr == 4) { double answer = num1 / num2; Console.WriteLine("\n\n" + answer); Console.ReadLine(); } if (mathOpr == 5) { double answer = Math.Pow(num1, num2); Console.WriteLine("\n\n" + answer); Console.ReadLine(); } if (mathOpr == 6) { if (num1 &lt; 0) { Console.WriteLine("\n\nNo real solutions"); } else { double answer = Math.Sqrt(num1); Console.WriteLine("\n\n" + answer); } } else { Console.WriteLine("That is not a valid option, idiot."); Console.ReadLine(); } } if (choice == 3) { Console.WriteLine("Enter initial amount: "); double initAmount = double.Parse(Console.ReadLine()); Console.WriteLine("Enter interest rate (ex. 6% = .06): "); double rate = double.Parse(Console.ReadLine()); Console.WriteLine("Enter time range (Year, Month, Week, etc.): "); string timeRange = Console.ReadLine(); Console.WriteLine("Enter amount of " + timeRange.ToLower() + "s: "); int time = int.Parse(Console.ReadLine()); for (int time2 = 1; time2 &lt;= time; time2++) { double totalAmount = initAmount * Math.Pow(1 + rate, time2); Console.WriteLine("\n" + timeRange + " " + time2 + " ---------- " + totalAmount); } Console.ReadLine(); } } catch { Console.WriteLine("That is not a valid option."); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload