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
Sum31stTo50thEven = Application.Sum(Application.Index([ROW(1:100)], [2*ROW(31:50)]))
Sub Sum31stto50thEvenNumbers()... and neither of these is correct.
Sub sumeven()
Dim loop_ctr&, even_ctr&, sum&
Do
even_ctr = loop_ctr * 2 + 31 * 2 ' start from 62 to 64,66,...
sum = sum + even_ctr ' running sum
loop_ctr = loop_ctr + 1
Loop Until even_ctr >= 100
MsgBox "Sum of even number is: " & sum
End Sub
You tell me. Your code gives the answer 1,620. Is that the answer you expect?Is this correct?
Sub Sum31stto50thEvenNumbers()
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
If (even_number_counter > 30 And even_number_counter <= 50) Then
sum = sum + loop_ctr
End If
End If
If (even_number_counter = 50) Then
Exit Do
End If
loop_ctr = loop_ctr + 1
Loop
MsgBox "Sum of 31st to 50th even numbers is : " & sum
End Sub
Cell Formulas | ||
---|---|---|
Range | Formula | |
E7 | E7 | =50/2*(50/2+1)-30/2*(30/2+1) |
E8 | E8 | =25*(25+1)-15*(15+1) |
G8 | G8 | =SUM(IF((Ser>30)*(Ser<51)*(MOD(Ser,2)=0),Ser)) |
H8 | H8 | =SUM(IF(MOD(ROW(INDIRECT(B1&":"&C1)),2)=0,ROW(INDIRECT(B1&":"&C1)))) |
I8 | I8 | =SUM(IF(MOD(ROW(INDIRECT(31&":"&50)),2)=0,ROW(INDIRECT(31&":"&50)))) |
gaus says 100*101/2 -30*31/2 = 5050-465 = 4585The sum of the positive numbers > 30 to 100 is 1620.