Learn to Program
Chapter 2
Numbers
How many hours are in a week?
Well, there twenty-four hours in a day, and seven days in a week, so multiplying 24 and 7 sounds good.
#Hours in a week
puts 24 * 7
Result:
How many minutes are in a century?
Well, we know how many hours are in a day and we know how many minutes are in an hour. We also know how many days are in a year and how many years are in a century. So we want to multiply hours in a day by minutes in an hour: 24 * 7. Then, we want to multiply that my the number of days in a century: 365 * 100
#Minutes in a century
puts (24 * 60) * (365 * 100)
Result:
How many seconds old are you on your fifth birthday?
This is slightly harder, but not by much. We just take our last equation and multiply by seconds in a minute and only use five years. To get seconds, take hours in a day and multiply by seconds: 24 * 60 * 60. You could use 24 * 360, but it's easier if you think hours_in_a_day * minutes_in_an_hour * seconds_in_a_minute.
#Seconds in five years
puts (24 * 60 * 60) * (365 * 5)
Result:
If I am 101 million seconds old, how old am I?
This is the same kind on problem, just in reverse. We take the number of seconds (101000000) and divide it by the number of seconds in a year.
#How old am I if I'm 101,000,000 seconds old?
puts 101000000 / ((24 * 60 * 60) * 365)
Result: