w3resource

Ruby Basic Exercises: Check two temperatures


Write a Ruby program to check two temperatures and return true if one is less than 0 and the other is greater than 100.

Ruby Code:

def temp(temp1, temp2)
    return ( temp1 < 0 && temp2 > 100 ) || ( temp1 > 100 && temp2 < 0 );
end
print temp(110, -1),"\n"
print temp(-1, 110),"\n"
print temp(2, 120)

Output:

true
true
false

Flowchart:

Flowchart: Check two temperatures

Ruby Code Editor:



Contribute your code and comments through Disqus.

Previous: Write a Ruby program to create a new string from a given string with the last character added at the front and back of the given string. The lenght of the given string must be 1 or more.
Next: Write a Ruby program to print 34 upto 41.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.