-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals2_practice_problems.rb
More file actions
139 lines (97 loc) · 2.77 KB
/
Copy pathconditionals2_practice_problems.rb
File metadata and controls
139 lines (97 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 1. Use a variable to store a number, then write a condition that prints 0 if the number is equal to 10, and prints -1 otherwise.
puts "Problem 1: "
number = 11
if number == 10
p 0
else
p -1
end
# 2. Use a variable to store a number, then write a condition that prints -1 if the number is less than 10, prints 1 if the number is greater than 10, and prints 0 if the number is equal to 10.
puts "Problem 2: "
number = 10
if number < 10
p -1
elsif number > 10
p 1
else
p 0
end
# 3. Use variables to store two numbers, then write a condition that prints 1 if the numbers are both less than 10, and prints 0 otherwise.
puts "Problem 3: "
number1 = 1
number2 = 1
if number1 < 10 && number2 < 10
p 1
else
p 0
end
# 4. Use a variable to store a number, then write a condition that prints 1 if the number is over 9000, and prints -1 otherwise.
puts "Problem 4: "
number = 10000
if number > 9000
p 1
else
p -1
end
# 5. Use a variable to store a number, then write a condition that prints 9 if the number is less than 10, prints 19 if the number is less than 20, prints 29 if the number is less than 30, and prints -1 otherwise (only one print statement should occur).
puts "Problem 5: "
number = 29
if number < 10
p 9
elsif number < 20
p 19
elsif number < 30
p 29
else
p -1
end
# 6. Use variables to store two numbers, then write a condition that prints 100 if either number is greater than 10, and prints -100 otherwise.
puts "Problem 6: "
number1 = 11
number2 = 1
if number1 > 10 || number2 > 10
p 100
else
p -100
end
# 7. Use a variable to store a number, then write a condition that prints 1776 if the number is less than 0, and prints 1979 otherwise.
puts "Problem 7: "
number = -10
if number < 0
p 1776
else
p 1979
end
# 8. Use a variable to store a number, then write a condition that prints 100 if the number equals 100, prints 99 if the number is equal to 99, and prints 0 otherwise.
puts "Problem 8: "
number = 99
if number == 100
p 100
elsif number == 99
p 99
else
p 0
end
# 9. Use variables to store two numbers, then write a condition that prints 1 if the first number is less than zero and the second number is greater than 0, and prints 0 otherwise.
puts "Problem 9: "
number = -1
number2 = 10
if number < 0 && number2 > 0
p 1
else
p 0
end
# 10. Use a variable to store a number, then write a condition that prints 5 if the number is greater than 80, prints 4 if the number is greater than 60, prints 3 if the number is greater than 40, prints 2 if the number is greater than 20, and prints 1 otherwise (only one print statement should occur).
puts "Problem 10: "
number = 100
if number > 80
p 5
elsif number > 60
p 4
elsif number > 40
p 3
elsif number > 20
p 2
else
p 1
end