File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 87
87
* [ Fibonacci] ( https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb )
88
88
* [ Pascal Triangle Ii] ( https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/pascal_triangle_ii.rb )
89
89
90
+ ## Electronics
91
+ * [ Ohms Law] ( https://github.com/TheAlgorithms/Ruby/blob/master/electronics/ohms_law.rb )
92
+
90
93
## Maths
91
94
* [ 3Nplus1] ( https://github.com/TheAlgorithms/Ruby/blob/master/maths/3nPlus1.rb )
92
95
* [ Abs] ( https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb )
Original file line number Diff line number Diff line change
1
+ # A ruby program for Ohms Law, which is used to calculate Voltage for the given Resistance and Current.
2
+ # Ohms Law -> V = I * R
3
+ # Reference: https://en.wikipedia.org/wiki/Ohm's_law
4
+
5
+ def ohms_law ( i , r )
6
+ if ( i > 0 && r > 0 )
7
+ "The voltage for given #{ i } ampheres current and #{ r } ohms resistance is #{ r * i } volts."
8
+ else
9
+ raise
10
+ end
11
+ rescue
12
+ "Error: Please provide valid inputs only!"
13
+ end
14
+
15
+ # Valid inputs
16
+ puts ( ohms_law ( 5 , 10 ) )
17
+ # The voltage for given 5 ampheres current and 10 ohms resistance is 50 volts.
18
+ puts ( ohms_law ( 2.5 , 6.9 ) )
19
+ # The voltage for given 2.5 ampheres current and 6.9 ohms resistance is 17.25 volts.
20
+ puts ( ohms_law ( 0.15 , 0.84 ) )
21
+ # The voltage for given 0.15 ampheres current and 0.84 ohms resistance is 0.126 volts.
22
+
23
+ # Invalid inputs
24
+ puts ( ohms_law ( 5 , -10 ) )
25
+ # Error: Please provide valid inputs only!
26
+ puts ( ohms_law ( -5 , -10 ) )
27
+ # Error: Please provide valid inputs only!
28
+ puts ( ohms_law ( 5 , "10" ) )
29
+ # Error: Please provide valid inputs only!
30
+ puts ( ohms_law ( "a" , 10 ) )
31
+ # Error: Please provide valid inputs only!
You can’t perform that action at this time.
0 commit comments