Created
September 11, 2017 19:28
-
-
Save Nitesh-Mishra/ccace58b3e12e9892064d5c8c06f4f17 to your computer and use it in GitHub Desktop.
Roots of quadratic equation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Roots of quadratic equation | |
# | |
# $ ruby roots_of_quadratic_equations.rb | |
# Equation will be in the format : ax^2 + bx + c | |
# Enter the value of a : | |
# 2 | |
# Enter the value of b : | |
# 3 | |
# Enter the value of c : | |
# 4 | |
# Roots are imaginary | |
# Roots are -4+4.8i, -4-4.8i | |
class QuadraticEquationRoots | |
def initialize | |
puts "Equation will be in the format : ax^2 + bx + c" | |
puts "Enter the value of a :" | |
a = gets.strip.to_i | |
puts "Enter the value of b :" | |
b = gets.strip.to_i | |
puts "Enter the value of c :" | |
c = gets.strip.to_i | |
roots_of_quadratic_equations(a, b, c) | |
end | |
def roots_of_quadratic_equations(a, b, c) | |
d = ((b*b) - (4*a*c)) | |
root_d = Math.sqrt(d.abs).round(2) | |
if d > 0 | |
puts "Roots are real and different" | |
x1 = (-b + root_d)/ 2*a | |
x2 = (-b - root_d)/ 2*a | |
puts "Roots are #{x1.round(2)} and #{x2.round(2)}" | |
elsif d == 0 | |
puts "Roots are real and same" | |
x1 = -b / 2*a | |
puts "Roots are #{x1}" | |
else | |
puts "Roots are imaginary" | |
puts "Roots are #{-b/2*a}+#{root_d/2*a}i, #{-b/2*a}#{-root_d/2*a}i" | |
end | |
end | |
end | |
roots = QuadraticEquationRoots.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment