VBA - Find sum of 31st - 50th even numbers

Robinhoods

New Member
Joined
Mar 14, 2022
Messages
5
Office Version
  1. 365
Platform
  1. 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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
This is my code which is error. It can’t find true answer
Sub SumEvenNumbers()
Dim loop_ctr As Integer
Dim even_number_counter As Integer
Dim sum As Integer

loop_ctr = 1
sum = 0

Do Until loop_ctr > 100
If (loop_ctr Mod 2 = 0) Then
even_number_counter = even_number_counter + 1
End If

If (even_number_counter > 30 And even_number_counter <= 50) Then
sum = sum + loop_ctr
End If

If (even_number_counter = 50) Then
Exit Do
End If
loop_ctr = loop_ctr + 1
Loop

MsgBox "Sum of even numbers is : " & sum
End Sub
 
Upvote 0
formula of Gauss (n*(n+1)/2
pur guess 50 * 51 - 30 * 31 = 1620
 
Upvote 0
VBA Code:
Sub loopje()
     ptr = 31
     Do
          som = som + 2 * ptr
          ptr = ptr + 1
     Loop While ptr <= 50
     MsgBox som
End Sub
 
Upvote 0
You'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.
 
Upvote 0
You'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.
what should I do
 
Upvote 0
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.
 
Upvote 0
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.
Sub SumEvenNumbers()

Dim loop_ctr As Integer
Dim even_number_counter As Integer
Dim sum As Integer

loop_ctr = 1
sum = 0

Do Until loop_ctr > 100
If (loop_ctr Mod 2 = 0) Then
even_number_counter = even_number_counter + 1
End If

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

If (even_number_counter = 50) Then
Exit Do
End If
loop_ctr = loop_ctr + 1
Loop

The answer is 420 but it needs to 410 whyyy
 
Upvote 0
Are you sure this test is correct?

Rich (BB code):
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

I suggest you put a breakpoint inside this loop. Your code is doing a couple of things incorrectly, which you should be able to see if you step through the code and test the values of even_number_counter and sum.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top