Introduction To Ruby
Performing Calculations

Example Programs

Enter and test the following program,

# Sum Of Two Numbers

print "Enter first number: "
a = gets.chomp
print "Enter second number: "
b = gets.chomp

answer = a + b
puts "a + b = #{answer}"

gets

When you run this program, you don't get the result that you expect. We've already seen a trick that could help us work out why. Change the code so that it reads as follows,

# Sum Of Two Numbers

print "Enter first number: "
a = gets.chomp
print "Enter second number: "
b = gets.chomp

answer = a + b
puts "a + b = #{answer}"

puts a.class
puts b.class
puts answer.class

gets

The extra lines tell us how Ruby is treating the numeric values that we entered. Ruby assumes that the user input will be a string. The addition operator that we used is actually a concatenation (adding strings) operator. Our two strings are joined together and we end up with something like 1 + 2 = 12.

In order to get Ruby to understand the user input as numeric, we will need to use a conversion method. Here is one way that we could rewrite the program,

# Sum Of Two Numbers

print "Enter first number: "
a = gets.chomp
print "Enter second number: "
b = gets.chomp

a = a.to_i
b = b.to_i

answer = a + b
puts "a + b = #{answer.to_s}"


gets

The two extra lines convert the input to integers. If the variables a and b store integers then the variable answer will automatically become an integer too.

The following conversions may also prove helpful,

a = a.to_s # convert to string
a = a.to_i # convert to integer
a = a.to_f # convert to float
a = a.to_c # convert to complex
a = a.to_r # convert to rational

Challenges

  1. Test the example program by typing in decimal numbers. Adapt it so that you can add together numbers with decimals.
  2. Adapt the example program so that it, (a) subtracts the second number from the first, (b) multiplies the numbers together, and (c) divides the first number by the second.
  3. Change the program so that it performs all of the operations in one program, displaying the results in a useful way.
  4. Write a program that uses integer division and the modulus operator to convert a number of seconds to minutes and seconds.
  5. Write a program that allows the user to input the radius of a circle and get the area and circumference. Create a constant for the value of PI and look up the formulas if you need to.
  6. A class has four exams in one term. Write a program that reads in these scores and returns the student's average mark.
  7. A temperature in degrees Celsius can be converted to degrees Fahrenheit using the formula F = (9/5) * C + 32. Write a program that allows the user to input a temperature in Celsius and output the equivalent in degrees Fahrenheit.
  8. Write a program that converts temperatures from degrees Fahrenheit to degrees Celsius.
  9. Freda is paid £4.80 and hour. This rate is for the first 35 hours that she works. Any overtime she works is paid at time and a half. Input the number of hours worked and calculate the wage due. Assume that she works more than 35 hours each week.
  10. Write a program that converts a fraction to a percentage (the user can enter the numerator and denominator separately).
  11. Write a program that calculates x% of a quantity y where x and y are values input by the user.