Ruby Basic Exercises: Compute and print the sum of two given integers
Ruby Basic: Exercise-44 with Solution
Write a Ruby program to compute and print the sum of two given integers, print 30 if the sum is in the range 20..30 (inclusive).
Ruby Code:
def sum(a,b)
sum = a + b;
if(sum >= 20 && sum <= 30)
return 20;
end
return sum;
end
print sum(9, 7),"\n"
print sum(12, 10),"\n"
print sum(22, 15)
Output:
16 20 37
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check if the sequence of numbers 10, 20, 30 appears anywhere in a given array of integers.
Next: Write a Ruby program to check two given integers and return 11 if either one is 11. Return their sum or difference if sum or difference is 11.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework