Advertisement
desislava_topuzakova

Conditional Statements – More Exercises - Solutions

Aug 22nd, 2024
184
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. Solutions - Conditional Statements – More Exercises
  2. -----------------------------------------------------------
  3. ' 01. Valid Triangle
  4. Sub ValidTriangle()
  5. Dim a As Integer, b As Integer, c As Integer
  6. a = Range("A1").Value
  7. b = Range("A2").Value
  8. c = Range("A3").Value
  9.  
  10. If a < b + c And b < a + c And c < a + b Then
  11. Range("B1").Value = "Valid Triangle"
  12. Else
  13. Range("B1").Value = "Invalid Triangle"
  14. End If
  15. End Sub
  16. -----------------------------------------------------------
  17. ' 02. Coffee Shop
  18. Sub CoffeeShop()
  19. Dim drink As String, extra As String
  20. Dim price As Double
  21.  
  22. drink = Range("A1").Value
  23. extra = Range("A2").Value
  24.  
  25. If drink = "coffee" Then
  26. price = 1
  27. ElseIf drink = "tea" Then
  28. price = 0.6
  29. End If
  30.  
  31. If extra = "sugar" Then
  32. price = price + 0.4
  33. End If
  34.  
  35. Range("A3").Value = "Final price: $" & price
  36. End Sub
  37. -----------------------------------------------------------
  38. ' 03. Marketplace
  39. Sub Marketplace()
  40. Dim product As String, day As String
  41. Dim price As Double
  42.  
  43. product = Range("A1").Value
  44. day = Range("A2").Value
  45.  
  46. Select Case product
  47. Case "Kiwi"
  48. If day = "Weekday" Then
  49. price = 2.2
  50. Else
  51. price = 2.5
  52. End If
  53. Case "Banana"
  54. If day = "Weekend" Then
  55. price = 2.7
  56. Else
  57. price = 2.0
  58. End If
  59. Case "Apple"
  60. If day = "Weekend" Then
  61. price = 1.6
  62. Else
  63. price = 1.2
  64. End If
  65. End Select
  66.  
  67. Range("A3").Value = price
  68. End Sub
  69. -----------------------------------------------------------
  70. ' 04. Bonus Score
  71. Sub BonusScore()
  72. Dim points As Integer
  73. points = Range("A1").Value
  74.  
  75. Select Case points
  76. Case 0 To 3
  77. points = points + 5
  78. Case 4 To 6
  79. points = points + 15
  80. Case 7 To 9
  81. points = points + 20
  82. End Select
  83.  
  84. Range("A2").Value = points
  85. End Sub
  86. -----------------------------------------------------------
  87. ' 05. Day Of Week
  88. Sub DayOfWeek()
  89. Dim dayNum As Integer
  90. dayNum = Range("A1").Value
  91.  
  92. Select Case dayNum
  93. Case 1: Range("A2").Value = "Monday"
  94. Case 2: Range("A2").Value = "Tuesday"
  95. Case 3: Range("A2").Value = "Wednesday"
  96. Case 4: Range("A2").Value = "Thursday"
  97. Case 5: Range("A2").Value = "Friday"
  98. Case 6: Range("A2").Value = "Saturday"
  99. Case 7: Range("A2").Value = "Sunday"
  100. Case Else: Range("A2").Value = "Error"
  101. End Select
  102. End Sub
  103. -----------------------------------------------------------
  104. ' 06. Vowel or Consonant
  105. Sub VowelOrConsonant()
  106. Dim letter As String
  107. letter = Range("A1").Value
  108.  
  109. If letter Like "[AEIOUaeiou]" Then
  110. Range("A2").Value = "Vowel"
  111. Else
  112. Range("A2").Value = "Consonant"
  113. End If
  114. End Sub
  115. -----------------------------------------------------------
  116. ' 07. Sorted Numbers
  117. Sub SortedNumbers()
  118. Dim num1 As Integer, num2 As Integer, num3 As Integer
  119. num1 = Range("A1").Value
  120. num2 = Range("A2").Value
  121. num3 = Range("A3").Value
  122.  
  123. If num1 < num2 And num2 < num3 Then
  124. Range("B1").Value = "Ascending"
  125. ElseIf num1 > num2 And num2 > num3 Then
  126. Range("B1").Value = "Descending"
  127. Else
  128. Range("B1").Value = "Not sorted"
  129. End If
  130. End Sub
  131. -----------------------------------------------------------
  132. ' 08. Number Type
  133. Sub NumberType()
  134. Dim num As Integer
  135. num = Range("A1").Value
  136.  
  137. If num > 0 Then
  138. Range("A2").Value = "positive"
  139. ElseIf num < 0 Then
  140. Range("A2").Value = "negative"
  141. Else
  142. Range("A2").Value = "zero"
  143. End If
  144. End Sub
  145. -----------------------------------------------------------
  146. ' 09. Product of Three Numbers
  147. Sub ProductOfThreeNumbers()
  148. Dim num1 As Double, num2 As Double, num3 As Double
  149. num1 = Range("A1").Value
  150. num2 = Range("A2").Value
  151. num3 = Range("A3").Value
  152.  
  153. If num1 = 0 Or num2 = 0 Or num3 = 0 Then
  154. Range("B1").Value = "zero"
  155. ElseIf (num1 * num2 * num3) > 0 Then
  156. Range("B1").Value = "positive"
  157. Else
  158. Range("B1").Value = "negative"
  159. End If
  160. End Sub
  161. -----------------------------------------------------------
  162. ' 10. Area of Figures
  163. Sub AreaOfFigures()
  164. Dim figureType As String
  165. figureType = Range("A1").Value
  166.  
  167. Select Case figureType
  168. Case "square"
  169. Dim side As Double
  170. side = Range("A2").Value
  171. Range("A4").Value = side * side
  172. Case "rectangle"
  173. Dim width As Double, length As Double
  174. width = Range("A2").Value
  175. length = Range("A3").Value
  176. Range("A4").Value = width * length
  177. Case "circle"
  178. Dim radius As Double
  179. radius = Range("A2").Value
  180. Range("A4").Value = WorksheetFunction.Pi() * radius * radius
  181. End Select
  182. End Sub
  183.  
Advertisement
Comments
  • IvanRaka
    306 days
    Comment was deleted
  • IvanRaka
    306 days
    # text 0.14 KB | 0 0
    1. This is correct format for result on rows 35 and 67
    2.  
    3. With Range("A3")
    4. .Value = price
    5. .NumberFormat = "0.00"
    6. End With
  • IvanRaka
    306 days
    # text 1.01 KB | 0 0
    1. In 10. Area of Figures again missing formating in results:
    2.  
    3. Sub Area_Of_Figures()
    4.  
    5. Dim figureType As String
    6. figureType = Range("A1").Value
    7.  
    8. Select Case figureType
    9. Case "square"
    10. Dim side As Double
    11. side = Range("A2").Value
    12. With Range("A4")
    13. .Value = side * side
    14. .NumberFormat = "0.00"
    15. End With
    16. Case "rectangle"
    17. Dim width As Double, length As Double
    18. width = Range("A2").Value
    19. length = Range("A3").Value
    20. With Range("A4")
    21. .Value = width * length
    22. .NumberFormat = "0.00"
    23. End With
    24. Case "circle"
    25. Dim radius As Double
    26. radius = Range("A2").Value
    27. Range("A4").Value = WorksheetFunction.Pi() * radius * radius
    28. With Range("A4")
    29. .Value = WorksheetFunction.Pi() * radius * radius
    30. .NumberFormat = "0.00"
    31. End With
    32. End Select
    33.  
    34. End Sub
Add Comment
Please, Sign In to add comment
Advertisement