Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Loops - More Exercises - Solutions
- -------------------------------------------
- ' 01. Power of Number
- Sub PowerOfNumber()
- Dim N As Integer, P As Integer
- N = Range("A1").Value
- P = Range("A2").Value
- Range("A3").Value = N ^ P
- End Sub
- -------------------------------------------
- ' 02. Multiplication Table
- Sub MultiplicationTable()
- Dim N As Integer, i As Integer
- N = Range("A1").Value
- For i = 1 To 10
- Range("B" & i).Value = N & " x " & i & " = " & N * i
- Next i
- End Sub
- -------------------------------------------
- ' 03. Biggest Number
- Sub BiggestNumber()
- Dim N As Integer, i As Integer
- Dim num As Integer, maxNum As Integer
- N = InputBox("Enter the amount of numbers:")
- For i = 1 To N
- num = InputBox("Enter number " & i & ":")
- If i = 1 Or num > maxNum Then
- maxNum = num
- End If
- Next i
- MsgBox "The biggest number is: " & maxNum
- End Sub
- -------------------------------------------
- ' 04. Vowel Sum
- Sub VowelSum()
- Dim N As Integer, i As Integer
- Dim ch As String, sum As Integer
- N = InputBox("Enter the count of characters:")
- For i = 1 To N
- ch = InputBox("Enter character " & i & ":")
- Select Case ch
- Case "a", "e", "i", "o", "u"
- sum = sum + 1
- Case "A", "E", "I", "O", "U"
- sum = sum + 1
- End Select
- Next i
- MsgBox "The sum of vowels is: " & sum
- End Sub
- -------------------------------------------
- ' 05. Division to 2, 3 and 4
- Sub DivisionStatistics()
- Dim N As Integer, i As Integer
- Dim num As Integer, count2 As Integer, count3 As Integer, count4 As Integer
- N = InputBox("Enter the count of numbers:")
- For i = 1 To N
- num = InputBox("Enter number " & i & ":")
- If num Mod 2 = 0 Then count2 = count2 + 1
- If num Mod 3 = 0 Then count3 = count3 + 1
- If num Mod 4 = 0 Then count4 = count4 + 1
- Next i
- MsgBox Format(count2 / N * 100, "0.00") & "%" & vbCrLf & _
- Format(count3 / N * 100, "0.00") & "%" & vbCrLf & _
- Format(count4 / N * 100, "0.00") & "%"
- End Sub
- -------------------------------------------
- ' 06. Special Number
- Sub SpecialNumber_DoWhile()
- Dim num As Integer, digit As Integer, originalNum As Integer
- Dim isSpecial As Boolean
- num = Range("A1").Value
- originalNum = num
- isSpecial = True
- Do While num > 0
- digit = num Mod 10
- If originalNum Mod digit <> 0 Then
- isSpecial = False
- Exit Do
- End If
- num = num \ 10
- Loop
- If isSpecial Then
- Range("A2").Value = originalNum & " is special"
- Else
- Range("A2").Value = originalNum & " is not special"
- End If
- End Sub
- -------------------------------------------
- ' 07. Special Bonus
- Sub SpecialBonus()
- Dim stopNum As Integer, prevNum As Integer, num As Integer
- stopNum = InputBox("Enter the stop number:")
- While num <> stopNum
- prevNum = num
- num = InputBox("Enter a number:")
- Wend
- If prevNum <> 0 Then
- Debug.Print prevNum * 1.2
- End If
- End Sub
- -------------------------------------------
- ' 08. Account Balance
- Sub AccountBalance()
- Dim balance As Double
- Dim transaction As String
- Dim amount As Double
- Do While True
- transaction = InputBox("Enter transaction amount or 'End' to finish:")
- If transaction = "End" Then Exit Do
- amount = CDbl(transaction)
- If amount > 0 Then
- Debug.Print "Increase: " & Format(amount, "0.00")
- Else
- Debug.Print "Decrease: " & Format(amount, "0.00")
- End If
- balance = balance + amount
- Loop
- MsgBox "Balance: " & Format(balance, "0.00")
- End Sub
- -------------------------------------------
- ' 09. Number In Range
- Sub NumberInRange()
- Dim num As Integer
- Do Until num >= 1 And num <= 100
- num = InputBox("Enter a number in the range [1 ... 100]:")
- Loop
- MsgBox "You entered: " & num
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement