Robinhoods
New Member
- Joined
- Mar 14, 2022
- Messages
- 5
- Office Version
- 365
- Platform
- MacOS
This is my homework : find sum of 31st - 50th even numbers between 1-100 by using either do-while or do-until loop. Pls help me
If (even_number_counter > 30 And even_number_counter <= 50) Then
sum = sum + loop_ctr
End If
what should I doYou're close ...
Your code is summing:
VBA Code:If (even_number_counter > 30 And even_number_counter <= 50) Then sum = sum + loop_ctr End If
... without checking that you are counting only the even numbers.
If (loop_ctr Mod 2 = 0) Then
If (even_number_counter > 30 And even_number_counter <= 50) Then
Sub SumEvenNumbers()As I said, you're close.
Your code correctly tests for even numbers:
VBA Code:If (loop_ctr Mod 2 = 0) Then
And it correctly tests that your even_number_counter is in the right range:
Code:If (even_number_counter > 30 And even_number_counter <= 50) Then
You just need to think how you put these two bits together so that you count only the even numbers (and not all the numbers) when the even number count is between 31 and 50.
If (even_number_counter > 30 And even_number_counter <= 50) And (even_number_counter Mod 2 = 0) Then
even_number_counter = even_number_counter + 1
sum = sum + even_number_counter
End If
... and neither of these is correct.The answer is 420 but it needs to 410 whyyy