Czech version
logolink

< Back to the list of lessons

Solving Quadratic Equations (y = ax2 + bx + c)

AlgortimyContent of the lesson:

  • Used Libraries and Variables
  • Inserting Parameters
  • Improving Values Input Using Try-Except
  • List of Equation Types Which Can Be Formed
  • The End of Program
  • The Whole Source Code

Used Libraries and Variables

You have to link the Math library because you will use the square root (sqrt function) which is defined in this library. Use variables for a, b and c parameters (whole numbers), discriminant (a whole number because of the parameters) and finally x1 and x2 as the roots of the quadratic equation. The x1 and x2 variables should be defined as extended data type because of the square root.

Inserting Parameters

Let user insert values at the beginning of your program to a, b and c variables.

Entering parameters
write('a=');
readln(a);

Use the same procedure for reading b and c parameters.

Improving Values Input Using Try-Except

Current input can be improved (by catching exceptions) - the principle is easy, you only have to use a try block where all dangerous lines of code should be written (a dangerous line is for example division by a variable - you can get division by zero). In case you place this operation into the try block and division by zero occurs, then your program will not crash but will switch itself inside the next except block which is executed only in case that something goes wrong. Using the except block you can inform users about a mistake and handle it.

List of Equation Types Which Can Be Formed

All you have to do to finish your program is to add conditions for all types of equations. You can get 7 equations according to input parameters so you have to solve all of them. Their list is available here; number 1 means a positive value, -1 means a negative value and 0 means zero.

equationabc
1000
200-1/1
30-1/1-1/0/1
4-1/100
5-1/101
6-1/10-1
7-1/1-1/0/1-1/1

The program always writes which parameters were inserted and also the prescription of the equation. In case the equation cannot be solved, a warning is displayed. The source code may appear to be long and complex but there should be no type of simple quadratic equation which cannot be solved by this program.

The End of Program

The end of the program follows:
writeln;
writeln('The program will be terminated after pressing ENTER key.');
readln;

The Whole Source Code

The whole source code is here:
program kvadraticka_rovnice;

{$APPTYPE CONSOLE}

uses
 SysUtils,
 Math;

var a,b,c:integer;
diskriminant:integer;
x1,x2:extended;

begin
{ TODO -oUser -cConsole Main : Insert code here }

 writeln('Welcome to the program to compute quadratic equations formed as y = ax^2 + bx + c');
 writeln;
 writeln('Insert all parameters please...');

 try
  write('a=');
  readln(a);
 except
  writeln('You have entered an invalid parameter, the zero value will be used instead - a=0');
  a:=0;
 end;

 try
  write('b=');
  readln(b);
 except
  writeln('You have entered an invalid parameter, the zero value will be used instead - b=0');
  b:=0;
 end;

 try
  write('c=');
  readln(c);
 except
  writeln('You have entered an invalid parameter, the zero value will be used instead - c=0');
  c:=0;
 end;

 if (a=0)AND(b=0)AND(c=0) then
 begin
  writeln('You entered parameters for the equation 0=0');
  writeln('X belongs to the set of real numbers.');
 end
 else if (a=0)AND(b=0) then
 begin
  writeln('You entered parameters for the equation 0=', c);
  writeln('There is no solution for the set of real numbers');
 end
 else if (a=0)AND(b<>0) then
 begin
  writeln('You entered parameters for the linear equation 0=', b, 'x+', c);
  writeln('X = ', -c/b:0:2);
 end
 else if (a<>0)AND(b=0)AND(c=0) then
 begin
  writeln('You entered parameters for the quadratic equation 0=', a, 'x^2');
  writeln('X1,2 = 0');
 end
 else if (a<>0)AND(b=0)AND(c>0) then
 begin
  writeln('You entered parameters for the quadratic equation 0=', a, 'x^2+', c);
  writeln('There is no solution for the set of real numbers');
 end
 else if (a<>0)AND(b=0)AND(c<0) then
 begin
  writeln('You entered parameters for the quadratic equation 0=', a, 'x^2-', -c);
  x1:=sqrt(-c/a);
  x2:=-x1;
  writeln('X1=', x1:0:2);
  writeln('X2=', x2:0:2);
 end
 else if (a<>0)AND(b<>0) then
 begin
  writeln('You entered parameters for the quadratic equation 0=', a, 'x^2+', b, 'x+', c)
  diskriminant:=b*b-4*a*c;
  if (diskriminant>=0) then
  begin
   x1:=(-b+sqrt(diskriminant))/(2*a);
   x2:=(-b-sqrt(diskriminant))/(2*a);
   writeln('X1=', x1:0:2);
   writeln('X2=', x2:0:2);
  end
  else
   writeln('There is no solution for the set of real numbers.');
 end;

 writeln;
 writeln('The program will be terminated after pressing ENTER key.');
 readln;
end.

Questions

  1. What does the try-except block do?
  2. Try to find a logical mistake inside the program and correct it.

Individual Task

Improve the program to be able to accept decimal values of parameters and to be able to compute the results without converting them to whole numbers.

webdesign, xhtml, css, php - Mgr. Michal Mikláš