w3resource

Ruby Basic Exercises: Compute the absolute difference between n and 33

Ruby Basic: Exercise-17 with Solution

Write a Ruby program to compute the absolute difference between n and 33 and return double the absolute difference if n is over 33.

Ruby Code:

def diff_33(n)
    n > 33 ? ((n-33)*2).abs : (n-33).abs
end
print diff_33(47),"\n" 
print diff_33(17)

Output:

28
16

Flowchart:

Flowchart: Compute the absolute difference between n and 33

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to test whether you are minor (Consider a child unless he or she is less than 18 years old.) or not.
Next: Write a Ruby program to find the maximum of two numbers.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby-exercises/basic/ruby-basic-exercise-17.php