Thursday, October 10, 2024

Pascal and Python program to find the roots of a quadric equation

Pascal program to find the roots of a quadric equation.

{find the roots of a Quadric equation ax2+bx+c=0}
program Quad;

var
  a,b,c :integer;
  d:real;

function delta(a:integer;b:integer;c:integer):real ;
begin
  delta:=sqr(b) - 4*a*c;
end;


Begin
  write('Enter the value for a ?');
  readln(a);
  write('Enter the value for b ?');
  readln(b);
  write('Enter the value for c ?');
  readln(c);
  d:= delta(a,b,c) ;
  if d> 0 then
     begin
          Writeln('You have more than one Answers');
          Write('The answer are ',((-1*b)+sqrt(d))/2*a:10:2);
          Writeln('    ',((-1*b)-sqrt(d))/2*a:10:2);
     end
  else
      if d=0 then
         begin
           Writeln('You have only one Answer');
           Writeln('The answer is ',((-1*b)/2*a):10:2);

         end
      else
         Writeln('You have no Answers');

  readln;


end.

Python Program to find the roots of a quadric equation.

import math
def delta(a,b,c):
    return b*b - 4*a*c

a=int(input("Enter Value for a?"))
b=int(input("Enter Value for b?"))
c=int(input("Enter Value for c?"))

d=delta(a,b,c)
if d>0:
    print("There are two answers  - ", ((-1*b)+ math.sqrt(d))/(2*a)," - ",((-1*b)- math.sqrt(d))/(2*a))
    
elif(d==0):
    print("There is only one answer -", (-1*b)/(2*a))

else:
    print("There is no answer")

Sample output


Pascal Program to find the roots of a quadric equation


Pascal - Quadric equation solution

No comments:

Post a Comment