How to find the sum of all the multiples of 3 or 5 below 10 in VB.NET

2 Answers

0 votes
Imports System

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim sum As Integer = 0

        For i As Integer = 3 To 10 - 1
            If i Mod 3 = 0 OrElse i Mod 5 = 0 Then
                Console.WriteLine(i)
                sum += i
            End If
        Next

        Console.Write("sum = " & sum)
    End Sub
End Class



' run:
'
' 3
' 5
' 6
' 9
' sum = 23
' 

 



answered Oct 11, 2023 by avibootz
0 votes
Imports System
 
Public Class Program
    Public Shared Function Summ(ByVal n As Integer) As Integer
        Return (n * (n + 1)) / 2
    End Function
 
    Public Shared Sub Main(ByVal args As String())
        Dim below10 As Integer = 9
         
        Dim sum As Integer = 3 * Summ(Math.Floor(below10 / 3)) + 5 * Summ(Math.Floor(below10 / 5)) - (3 * 5) * Summ(Math.Floor(below10 / 15))
         
        Console.Write("sum = " & sum)
    End Sub
End Class
 
 
 
' run:
'
' sum = 23
'

 



answered Oct 11, 2023 by avibootz
edited Oct 13, 2023 by avibootz

Related questions

2 answers 145 views
2 answers 158 views
3 answers 187 views
2 answers 172 views
2 answers 146 views
2 answers 160 views
2 answers 138 views
...