Skip to content

Instantly share code, notes, and snippets.

@armw4
Created November 10, 2016 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armw4/f42e43f31d88cb15cf7f5505c282f99a to your computer and use it in GitHub Desktop.
Save armw4/f42e43f31d88cb15cf7f5505c282f99a to your computer and use it in GitHub Desktop.
Prints out a multiplication table. Written in Ruby for brevity. .NET equivalents in comments.
## To execute, install Ruby, download file, and run:
# ruby mult-table.rb
THREE_SPACES = ' ' * 3 # new String(' ', 3)
def print_header(rows)
print THREE_SPACES # Console.Write(...)
1.upto(rows) { |number| print "#{THREE_SPACES} #{number}" } # String.Format(...)
puts # Console.WriteLine(...)
end
def print_rows(rows)
1.upto(rows) do |row| # Enumerable.Range(...)
print "#{THREE_SPACES} #{row}"
1.upto(rows) { |number| print "#{THREE_SPACES} #{row * number}" }
puts
end
end
def print_table(rows)
print_header(rows)
print_rows(rows)
end
print_table(3) ## => 1 2 3
# 1 1 2 3
# 2 2 4 6
# 3 3 6 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment