3 Replies - 2690 Views - Last Post: 26 February 2012 - 01:49 AM

#1 PixelCard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 139
  • Joined: 20-June 08

Solve a quadratic equation

Posted 19 July 2008 - 10:37 AM

Description: This snippet was created and tested in a standard C# console application, so it will work in any other type of C# applications (WinForms, WPF) with some minor modifications.This code snippet solves a quadratic equation.
using System;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            // a, b and c are the coefficients of the equation.
            int a, b, c;
            // d is the discriminant.
            int d;
            // x1 and x2 are the solutions.
            double x1, x2;

            Console.WriteLine("The equation structure: axx+bx+c=0");

            // Get the value of a
            entA:Console.Write("Enter a:");
            try{
                // Convert the input to integer.
                a = Convert.ToInt32(Console.ReadLine());
            }
            catch{
                // In case of invalid input, show an error message and return
                // to input.
                Console.WriteLine("Invalid Data!");
                goto entA;
            }
            // Get the value of b
            entB:Console.Write("Enter b:");
            try{
                // Convert the input to integer.
                b = Convert.ToInt32(Console.ReadLine());
            }
            catch{
                // In case of invalid input, show an error message and return
                // to input.
                Console.WriteLine("Invalid Data!");
                goto entB;
            }
            // Get the value of c
            entC:Console.Write("Enter c:");
            try{
                // Convert the input to integer.
                c = Convert.ToInt32(Console.ReadLine());
            }
            catch{
                // In case of invalid input, show an error message and return
                // to input.
                Console.WriteLine("Invalid Data!");
                goto entC;
            }

            // Calculate the discriminant.
            d = b*b - 4 * a * c;

            // Verify the discriminant.
            if (d == 0) // If the discriminant is 0, both solutions are equal.
            {
                x1 = x2 = -b / (2 * a);
                Console.WriteLine("Both solutions are {0}.", x1);
                Console.ReadLine();
            }
            else if (d < 0) // If the discriminant is negative, there are no solutions.
            {
                Console.WriteLine("No solutions for the equation!");
                Console.ReadLine();
            }
            else // In other cases the discriminant is positive, so there are two different solutions.
            {
                x1 = (-b - Math.Sqrt(d) )/ (2 * a);
                x2 = (-b + Math.Sqrt(d)) / (2 * a);
                Console.WriteLine("The solutions are {0} and {1}.", x1, x2);
                Console.ReadLine();
            }       
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Solve a quadratic equation

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Solve a quadratic equation

Posted 05 December 2008 - 06:36 AM

maybe the code could be modified to not use labels and "goto" statements.
Was This Post Helpful? 0
  • +
  • -

#3 gardhiwasam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-July 11

Re: Solve a quadratic equation

Posted 01 July 2011 - 02:49 PM

I created a similar program: http://www.youtube.c...nel_video_title
Was This Post Helpful? 0
  • +
  • -

#4 danieltz96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-February 12

Re: Solve a quadratic equation

Posted 26 February 2012 - 01:49 AM

ok but when it finish calculation the console turn of.How to make it to prepare for next calculation
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1