Advertisement
desislava_topuzakova

Loops - More Exercises - Solutions

Aug 26th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. Loops - More Exercises - Solutions
  2. -------------------------------------------
  3. ' 01. Power of Number
  4. Sub PowerOfNumber()
  5. Dim N As Integer, P As Integer
  6. N = Range("A1").Value
  7. P = Range("A2").Value
  8. Range("A3").Value = N ^ P
  9. End Sub
  10. -------------------------------------------
  11. ' 02. Multiplication Table
  12. Sub MultiplicationTable()
  13. Dim N As Integer, i As Integer
  14. N = Range("A1").Value
  15.  
  16. For i = 1 To 10
  17. Range("B" & i).Value = N & " x " & i & " = " & N * i
  18. Next i
  19. End Sub
  20. -------------------------------------------
  21. ' 03. Biggest Number
  22. Sub BiggestNumber()
  23. Dim N As Integer, i As Integer
  24. Dim num As Integer, maxNum As Integer
  25.  
  26. N = InputBox("Enter the amount of numbers:")
  27.  
  28. For i = 1 To N
  29. num = InputBox("Enter number " & i & ":")
  30. If i = 1 Or num > maxNum Then
  31. maxNum = num
  32. End If
  33. Next i
  34.  
  35. MsgBox "The biggest number is: " & maxNum
  36. End Sub
  37. -------------------------------------------
  38. ' 04. Vowel Sum
  39. Sub VowelSum()
  40. Dim N As Integer, i As Integer
  41. Dim ch As String, sum As Integer
  42.  
  43. N = InputBox("Enter the count of characters:")
  44.  
  45. For i = 1 To N
  46. ch = InputBox("Enter character " & i & ":")
  47. Select Case ch
  48. Case "a", "e", "i", "o", "u"
  49. sum = sum + 1
  50. Case "A", "E", "I", "O", "U"
  51. sum = sum + 1
  52. End Select
  53. Next i
  54.  
  55. MsgBox "The sum of vowels is: " & sum
  56. End Sub
  57. -------------------------------------------
  58. ' 05. Division to 2, 3 and 4
  59. Sub DivisionStatistics()
  60. Dim N As Integer, i As Integer
  61. Dim num As Integer, count2 As Integer, count3 As Integer, count4 As Integer
  62.  
  63. N = InputBox("Enter the count of numbers:")
  64.  
  65. For i = 1 To N
  66. num = InputBox("Enter number " & i & ":")
  67. If num Mod 2 = 0 Then count2 = count2 + 1
  68. If num Mod 3 = 0 Then count3 = count3 + 1
  69. If num Mod 4 = 0 Then count4 = count4 + 1
  70. Next i
  71.  
  72. MsgBox Format(count2 / N * 100, "0.00") & "%" & vbCrLf & _
  73. Format(count3 / N * 100, "0.00") & "%" & vbCrLf & _
  74. Format(count4 / N * 100, "0.00") & "%"
  75. End Sub
  76. -------------------------------------------
  77. ' 06. Special Number
  78. Sub SpecialNumber_DoWhile()
  79. Dim num As Integer, digit As Integer, originalNum As Integer
  80. Dim isSpecial As Boolean
  81. num = Range("A1").Value
  82. originalNum = num
  83. isSpecial = True
  84.  
  85. Do While num > 0
  86. digit = num Mod 10
  87. If originalNum Mod digit <> 0 Then
  88. isSpecial = False
  89. Exit Do
  90. End If
  91. num = num \ 10
  92. Loop
  93.  
  94. If isSpecial Then
  95. Range("A2").Value = originalNum & " is special"
  96. Else
  97. Range("A2").Value = originalNum & " is not special"
  98. End If
  99. End Sub
  100. -------------------------------------------
  101. ' 07. Special Bonus
  102. Sub SpecialBonus()
  103. Dim stopNum As Integer, prevNum As Integer, num As Integer
  104. stopNum = InputBox("Enter the stop number:")
  105.  
  106. While num <> stopNum
  107. prevNum = num
  108. num = InputBox("Enter a number:")
  109. Wend
  110.  
  111. If prevNum <> 0 Then
  112. Debug.Print prevNum * 1.2
  113. End If
  114. End Sub
  115.  
  116. -------------------------------------------
  117. ' 08. Account Balance
  118. Sub AccountBalance()
  119. Dim balance As Double
  120. Dim transaction As String
  121. Dim amount As Double
  122.  
  123. Do While True
  124. transaction = InputBox("Enter transaction amount or 'End' to finish:")
  125. If transaction = "End" Then Exit Do
  126. amount = CDbl(transaction)
  127. If amount > 0 Then
  128. Debug.Print "Increase: " & Format(amount, "0.00")
  129. Else
  130. Debug.Print "Decrease: " & Format(amount, "0.00")
  131. End If
  132. balance = balance + amount
  133. Loop
  134.  
  135. MsgBox "Balance: " & Format(balance, "0.00")
  136. End Sub
  137. -------------------------------------------
  138. ' 09. Number In Range
  139. Sub NumberInRange()
  140. Dim num As Integer
  141.  
  142. Do Until num >= 1 And num <= 100
  143. num = InputBox("Enter a number in the range [1 ... 100]:")
  144. Loop
  145.  
  146. MsgBox "You entered: " & num
  147. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement