Created
April 8, 2014 05:38
-
-
Save tbuchanan/10094707 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Temperature converter | |
print "Enter 1 to convert Fahrenheit to Celsius or 2 to convert Celsius to Fahrenheit: " | |
user_input = Integer(gets.chomp) | |
if user_input == 1 | |
print "Great choice! Now enter the temperature you would like to convert: " | |
temp = Integer(gets.chomp) | |
convert_to_C = (temp - 32) * (5/9) | |
puts "Your temperature is #{convert_to_C} degrees Celsius." | |
elsif user_input == 2 | |
print "Great choice! Now enter the temperature you would like to convert: " | |
temp = Integer(gets.chomp) | |
convert_to_F = (temp + 32) * (9/5) | |
puts "Your temperature is #{convert_to_F} degrees Fahrenheit." | |
else | |
puts "Oops, please retry the converter and only enter the numbers 1 or 2 this time." | |
end | |
#GUESSING GAME | |
random = Random.rand(100) | |
#p random (used to identify number to test code) | |
print "Please guess a number between 1 & 100: " | |
user_number = Integer(gets.chomp) | |
counter = 1 | |
while user_number != random | |
if user_number > random | |
puts "The number is lower than #{user_number}. Guess again:" | |
counter += 1 | |
user_number = Integer(gets.chomp) | |
elsif user_number < random | |
puts "The number is higher than #{user_number}. Guess again:" | |
user_number = Integer(gets.chomp) | |
counter += 1 | |
end | |
if user_number == random | |
puts "You got it in #{counter} tries!" | |
end | |
end | |
#SIMPLE ASCII ART | |
print "Please enter a number for how many rows high you want your snowflake XMAS tree to be: " | |
num_rows = Integer(gets.chomp) | |
rows = 0 | |
snowflake = 1 | |
space = (50 - snowflake) / 2 | |
until num_rows == rows | |
puts (" " * space) + ("*" * snowflake) | |
snowflake += 2 | |
space -= 1 | |
rows += 1 | |
end | |
#REVERSE A STRING | |
print "Type some text: " | |
string = gets | |
string2 = "" | |
string.each_char {|char| string2 = char << string2} | |
puts string2 | |
#MULTIPLICATION TABLE | |
puts "A multiplication table: " | |
puts | |
for table_range in 1..9 | |
print " #{table_range} " | |
end | |
puts | |
puts | |
for number1 in 1..9 | |
print number1, "| " | |
for number2 in 1..9 | |
product = (number2 * number1) | |
if product >= 10 | |
print product, " " | |
elsif product < 10 | |
print product, " " | |
end | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment