Translate

Friday 18 April 2014

Quadratic Equations Solver in C#

Quadratic equation solver console application

Your Quadratic equation must be in starndard form
ax2 + bx + c = 0
This console application will promt you to enter the value of a
Enter the value of a then press entter

And then it will prompt you to enter the value of b
Enter the value of b then press entter

Finaly it will prompt you to enter the value of c
Enter the value of c then press enter



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace QuadraticEquations

{

    class QuadraticEquations

    {

        public static double a;

        public static double b;

        public static double c;



        public void QuadRoots ()

        {

            string option;

            double Formula;

            double x1;

            double x2;         

            //Colecting user input

            Console.WriteLine("Enter a");

            a = Convert.ToDouble(Console.ReadLine());

            //Colecting user input

            Console.WriteLine("Enter b");

            b = Convert.ToDouble(Console.ReadLine());

            //Colecting user input

            Console.WriteLine("Enter c");

            c = Convert.ToDouble(Console.ReadLine());

            //Calculating the roots of the equation using quadratic formula

            Formula = Convert.ToDouble(Math.Sqrt(b * b - 4 * a * c));

            x1 = -b - Formula;

            x2 = -b + Formula;

            x1 /= (2 * a);

            x2 /= (2 * a);

            //displaying roots of the quadratic equation           

            Console.WriteLine("\t X1 = " + x1);

            Console.WriteLine("\t X2 = " + x2 + "\n");

            Console.WriteLine("\n 1.Back");

            Console.WriteLine(" 2.Exit");

            Console.WriteLine(" 3.Quadratic equation solver");

            option = Console.ReadLine();

            Console.WriteLine("****************************
********************************************************");

          

            switch (option)

            {

                case "1": Console.Clear();

                    Main();

                    break;

                case "2": Console.Clear();

                    break;

                case "3": Console.Clear();

                    QuadRoots();

                    break;

                default: Console.WriteLine("Invalid choice");

                    QuadRoots();

                    break;

            }

        }



        public void Disclaimer()

        {

            string option;

            Console.WriteLine("Enter a, b and c of quadratic equation in this form ax^2 + bx + c");

            Console.WriteLine(" e.g -2x^2 - 6x + 20 = 0");

            Console.WriteLine("You will be prompt to enter a , you just type -2 and press the Enter button");

            Console.WriteLine("You will be prompt to enter b , you just type -6 and press the Enter button");

            Console.WriteLine("You will be prompt to enter c , you just type 20 and press the Enter button");

            Console.WriteLine("The roots will be displayed immediately after you finished ");

            Console.WriteLine("Please note this application does not returns factions, eg if x = 1/2, \n x will be x = 0,5 \n");

            Console.WriteLine("\n 1.Back");

            Console.WriteLine(" 2.Exit");

            option = Console.ReadLine();         

            switch (option)

            {

                case "1": Console.Clear();

                    Main();

                    break;

                case "2": Console.Clear();                   

                    break;

                default: Console.WriteLine("Invalid choice");

                    Disclaimer();

                    break;

            }

        }

      

        static void Main()

        {

            Console.Title="Quadratic Equation Formula";           

            QuadraticEquations s1 = new QuadraticEquations();

            Console.ForegroundColor = ConsoleColor.Red;

            string option;

            Console.WriteLine("*************************
********************************************");

            Console.WriteLine("Enter the number that corresponds with your choice of action and the press the  Enter keypad " + "\n");

            Console.WriteLine("1.Quadratic equation solver");

            Console.WriteLine("2.Exit");

            Console.WriteLine("3.Clear screen");

            Console.WriteLine("4.Please read me");

            option = Console.ReadLine();

            Console.WriteLine("*********************
************************************************");



          

            switch (option)

            {

                case "1": s1.QuadRoots();

                    break;

                case "2": Console.Clear();

                    break;

                case "3": Console.Clear();

                    Main();

                    break;

                case "4": s1.Disclaimer();

                    break;

                default: Console.WriteLine("Invalid choice");

                    Main();

                    break;

            }

        }

    }

}


Please note that this console application works with decimals and whole numbers it does not work with fractions

No comments :