Program in Icon
procedure Quadratic_Roots ( a, b, c )
# Generates the real roots of ax^2+bx+c=0.
local d # Discriminant
d := b ^ 2 - 4.0 * a * c; # Compute the discriminant
if d > 0 then
{ #-- Two roots
suspend ( - b + sqrt ( d ) ) / ( 2.0 * a );
suspend ( - b - sqrt ( d ) ) / ( 2.0 * a );
} #-- Two roots
else if d = 0 then
suspend - b / ( 2.0 * a );
fail; # No more roots
end
procedure main ()
local root # Holds each root generated by the solver
local count # Counts the number of roots returned
count := 0;
every root := Quadratic_Roots ( 1, -4, 3 ) do
{ #-- The braces {} group the next two statements together
write ( "One solution is: x = ", root );
count +:= 1; # Count the roots generated
}
write ( "The number of solutions was ", count );
end