Lab 4 - Correction
Lab 4 - Correction
Lab 4
Loops
Exercise 1:
Write VBA code to calculate the sum of numbers from 1 to 100 using:
1. A For loop.
2. A While loop.
3. A Do-While loop.
Sub SumOfNumbers()
Dim i As Integer
Dim sumFor As Integer
Dim sumWhile As Integer
Dim sumDoWhile As Integer
Write VBA code to find the first multiple of 7 greater than a given number in A1 using:
1. A While loop.
2. A Do-While loop.
Sub FirstMultipleOf7()
Dim number As Integer
Dim result As Integer
number = Range("A1").Value
Write VBA code to count the even numbers between 1 and a given number in A1 using:
1. A For loop.
2. A While loop.
3. A Do-While loop.
Sub CountEvenNumbers()
Dim i As Integer
Dim countFor As Integer
Dim countWhile As Integer
Dim countDoWhile As Integer
Dim limit As Integer
limit = Range("A1").Value
attempts = 0
Do
password = InputBox("Enter the password:")
attempts = attempts + 1
Exercise 5:
1. A For loop.
2. A While loop.
Sub Factorial()
Dim i As Integer
Dim factorialFor As Double
Dim factorialWhile As Double
Dim number As Integer
number = Range("A1").Value
1. A For loop.
2. A While loop.
Sub ProductOfNumbers()
Dim i As Integer
Dim productFor As Double
Dim productWhile As Double
Find the smallest square (e.g., 1², 2², etc.) greater than a given number in A1 using:
1. A While loop.
2. A Do-While loop.
Sub SmallestSquare()
Dim number As Double
Dim square As Integer
Dim i As Integer
number = Range("A1").Value
Count how many numbers between 1 and a given number in A1 are divisible by 3 using:
1. A For loop.
2. A While loop.
Sub CountDivisibleBy3()
Dim i As Integer
Dim countFor As Integer
Dim countWhile As Integer
Dim limit As Integer
limit = Range("A1").Value
Exercise 9:
Write a VBA code to repeat a message “Hello, VBA!” in column A for a given number of times
specified in A1 using the appropriate loop.
Sub RepeatMessage()
Dim i As Integer
Dim count As Integer
count = Range("A1").Value
Write a VBA code to calculate the sum of all odd numbers between 1 and a given number in
A1 using:
1. A For loop.
2. A While loop.
Sub SumOddNumbers()
Dim i As Integer
Dim sumFor As Integer
Dim sumWhile As Integer
Dim limit As Integer
limit = Range("A1").Value