TheAlgorithms-Ruby/electronics/ohms_law.rb

32 lines
1 KiB
Ruby
Raw Normal View History

2021-09-04 12:46:22 +02:00
# A ruby program for Ohms Law, which is used to calculate Voltage for the given Resistance and Current.
# Ohms Law -> V = I * R
# Reference: https://en.wikipedia.org/wiki/Ohm's_law
def ohms_law(i, r)
2021-11-02 07:45:20 +01:00
if i > 0 && r > 0
2021-09-04 12:46:22 +02:00
"The voltage for given #{i} ampheres current and #{r} ohms resistance is #{r * i} volts."
else
raise
end
2021-11-02 07:45:20 +01:00
rescue StandardError
'Error: Please provide valid inputs only!'
2021-09-04 12:46:22 +02:00
end
# Valid inputs
puts(ohms_law(5, 10))
# The voltage for given 5 ampheres current and 10 ohms resistance is 50 volts.
puts(ohms_law(2.5, 6.9))
# The voltage for given 2.5 ampheres current and 6.9 ohms resistance is 17.25 volts.
puts(ohms_law(0.15, 0.84))
# The voltage for given 0.15 ampheres current and 0.84 ohms resistance is 0.126 volts.
# Invalid inputs
puts(ohms_law(5, -10))
# Error: Please provide valid inputs only!
puts(ohms_law(-5, -10))
# Error: Please provide valid inputs only!
2021-11-02 07:45:20 +01:00
puts(ohms_law(5, '10'))
2021-09-04 12:46:22 +02:00
# Error: Please provide valid inputs only!
2021-11-02 07:45:20 +01:00
puts(ohms_law('a', 10))
2021-09-04 12:46:22 +02:00
# Error: Please provide valid inputs only!