Skip to content

Instantly share code, notes, and snippets.

@benbek
Created December 9, 2016 22:30
Show Gist options
  • Save benbek/906334ce5027be167b676ec3befcd69b to your computer and use it in GitHub Desktop.
Save benbek/906334ce5027be167b676ec3befcd69b to your computer and use it in GitHub Desktop.
Quadratic equation solver homework, March 2003
{ INPUT: The coefficients and terms
OUTPUT: The equation with its roots, if applicable }
Program Equation;
Uses Crt;
Var A, B, C : Integer; { Stores the parameters }
Ax,Bx,Cx: String; {Temporarly stores the parameters}
Desc : Integer; { Stores the discriminant }
X1 : Real; { Stores the first root }
X2 : Real; { Stores the second root }
ValCode : Integer; {Required for Val to work}
Begin
ClrScr;
X1:=0;
X2:=0;
TextColor(Red);
Write('Enter the A coefficient: ');
TextColor(LightGray);
ReadLn(Ax);
Val(Ax,A,ValCode);
TextColor(Green);
If A=0
Then WriteLn(' This is a linear equation.')
Else WriteLn(' This is a quadratic equation.');
TextColor(Red);
Write('Enter the B coefficient: ');
TextColor(LightGray);
ReadLn(Bx);
Val(Bx,B,ValCode);
TextColor(Red);
Write('Enter the C term: ');
TextColor(LightGray);
ReadLn(Cx);
Val(Cx,C,ValCode);
If A<>0 { The equation isn't linear } Then
Begin
Desc:=(Sqr(B)+(-4*A*C));
TextColor(LightBlue);
WriteLn(' The value of the discriminant is ',Desc,'.');
If Desc>0
Then Begin
X1:=((B*-1)+Sqrt(Desc))/(2*A);
X2:=((B*-1)-Sqrt(Desc))/(2*A);
End
Else If Not Desc<0
Then Begin
X1:=((B*-1)+Sqrt(Desc))/(2*A);
End;
End
Else X1:=C/-B;
TextColor(White);
WriteLn('ֽֽֽֽֽֽֽֽֽֽֽֽֽֽֽֽ');
WriteLn('The equation you have typed is:');
TextColor(Yellow);
Write(' ',A);
TextColor(White);
Write('X‎ ');
TextColor(Yellow);
If Not (B<0) Then Write('+');
Write(B);
TextColor(White);
Write('X ');
TextColor(Yellow);
If Not (C<0) Then Write('+');
Write(C);
TextColor(White);
WriteLn(' = 0');
If X1=0
Then WriteLn('It has no roots.')
Else Begin
WriteLn('Its root(s) are:');
WriteLn(' X1 = ',X1:0:2);
If X2<>0 Then WriteLn(' X2 = ',X2:0:2);
End;
WriteLn;
WriteLn('Press any key to exit...');
Repeat
Until KeyPressed;
End.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment