From 3afa6d07376cbc2630cb899d82a40531d9567b8d Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi Date: Sat, 4 Sep 2021 16:16:22 +0530 Subject: [PATCH] ohms law implementation --- DIRECTORY.md | 3 +++ electronics/ohms_law.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 electronics/ohms_law.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 4e12d72..b2d6759 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -87,6 +87,9 @@ * [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb) * [Pascal Triangle Ii](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/pascal_triangle_ii.rb) +## Electronics + * [Ohms Law](https://github.com/TheAlgorithms/Ruby/blob/master/electronics/ohms_law.rb) + ## Maths * [3Nplus1](https://github.com/TheAlgorithms/Ruby/blob/master/maths/3nPlus1.rb) * [Abs](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb) diff --git a/electronics/ohms_law.rb b/electronics/ohms_law.rb new file mode 100644 index 0000000..bececb9 --- /dev/null +++ b/electronics/ohms_law.rb @@ -0,0 +1,31 @@ +# 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) + if(i > 0 && r > 0) + "The voltage for given #{i} ampheres current and #{r} ohms resistance is #{r * i} volts." + else + raise + end + rescue + "Error: Please provide valid inputs only!" +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! +puts(ohms_law(5, "10")) +# Error: Please provide valid inputs only! +puts(ohms_law("a", 10)) +# Error: Please provide valid inputs only!