Kadri Mohamed Hamza
Kadri Mohamed Hamza
% Check if a is zero
if a == 0
disp('Error: Coefficient a cannot be zero for a quadratic equation.');
else
% Compute the discriminant
discriminant = b^2 - 4*a*c;
disp('The discriminant is: ')
disp(discriminant);
% Determine the nature of the roots based on the discriminant
if discriminant > 0
% Two real roots
root1 = (-b + sqrt(discriminant)) / (2*a); root2 = (-b - sqrt(discriminant)) / (2*a);
disp(['root1 = ', num2str(root1), ', root2 = ', num2str(root2)]);
elseif discriminant == 0
% One real root
root = -b / (2*a);disp(['One real root: x = ', num2str(root)]);
else
% No real roots (negative discriminant)
disp('No real solution exists (discriminant < 0)');
end
end