3

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.

Properly working code (Thanks, Gunther Fox):

if(discriminant < 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();
}

Old 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 < 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 < 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 <= 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.");
        }
    }
}
yoozer8
  • 7,361
  • 7
  • 58
  • 93
pcnThird
  • 2,362
  • 2
  • 19
  • 33

2 Answers2

3

If the discriminant is negative, can you just flip the sign of the discriminant, do the root calculation, then add the i to the end of the result?

3

Flip the discriminate and multiply by i:

double discriminate = (b*b)-(4*a*c);
if(discriminate < 0){
    double x1 = -b/(2*a);
    discriminate = -discriminate ;
    double x2 = Math.Sqrt(discriminate)/(2*a);
    double x3 = -Math.Sqrt(discriminate)/(2*a);
}
Console.WriteLine(x1.ToString() + " + " + x2.ToString() + " * i ");
Console.WriteLine(x1.ToString() + " + " + x3.ToString() + " * i ");
Foggzie
  • 9,691
  • 1
  • 31
  • 48
  • Is this what you meant: if (discriminant < 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 + (x2 * Math.Sqrt(-1))); Console.WriteLine(x1 + (x3 * Math.Sqrt(-1))); Console.ReadLine(); } – pcnThird Dec 20 '12 at 00:35
  • The program displays NaN(Not a number), unfortunately, or maybe I'm doing something wrong. Before this, I tried this: http://www.vcskicks.com/quadratic-formula.php, but it still showed NaN as the answer. – pcnThird Dec 20 '12 at 00:39
  • That's because the answer is Not a real number. The value of Math.Sqrt(-1) cannot be represented by a float or double. You're WriteLines are still trying to calculate sqrt(-1). Instead, just append "*i" to the end of your string. – Foggzie Dec 20 '12 at 00:41
  • Since 'i' is equivalent to the square root of (-1) I figured Math.Sqrt(-1) would also work, but I overlooked the use of 'double' in this case. So what's the proper way to do this? Thanks. – pcnThird Dec 20 '12 at 00:44
  • "That's because the answer is Not a real number. The value of Math.Sqrt(-1) cannot be represented by a float or double. You're WriteLines are still trying to calculate sqrt(-1). Instead, just append "*i" to the end of your string.". I see. I'll give it a try now. – pcnThird Dec 20 '12 at 00:45
  • You simply don't put the answer in a double. You format it as a string. I edited my answer again. – Foggzie Dec 20 '12 at 00:45