Menu
CoddyTech

Integer Arithmetic

Lesson 4 of 48 in Coddy's Ruby for PHP Developers course.

PHP intdiv truncates toward zero, and remainder follows the dividend sign. Ruby integer division floors, making negative quotients and remainders differ. PHP integer width depends on the platform and overflowing arithmetic can become floating point; Ruby integers expand as needed. Choose explicit conversion when floating division is intended.

PHP: complete program

<?php
echo intdiv(-7, 3), "\n", -7 % 3, "\n", 7 / 2, "\n";

Ruby: complete program

puts(-7 / 3)
puts(-7 % 3)
puts(7.0 / 2)
challenge icon

Challenge

Easy

Input is nonnegative minutes up to 100000. Return hours and remaining minutes separated by a colon, without zero padding.

Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.

Your code goes in exercise.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'exercise'
def check(value)
  raise 'Public API check failed' unless value
end

puts solve(STDIN.read)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Ruby for PHP Developers

Practice on your own: Online Ruby compiler