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.

  1. #Hours in a week

  2. puts 24 * 7

Result:

168

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

  1. #Minutes in a century

  2. puts (24 * 60) * (365 * 100)

Result:

52560000

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.

  1. #Seconds in five years

  2. puts (24 * 60 * 60) * (365 * 5)

Result:

157680000

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.

  1. #How old am I if I'm 101,000,000 seconds old?

  2. puts 101000000 / ((24 * 60 * 60) * 365)

Result:

3