Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,919 questions

51,852 answers

573 users

How to sum of all the prime numbers in an array with VB.NET

1 Answer

0 votes
Imports System

Public Class Test
    Public Shared Function isPrime(n As Integer) As Boolean
        If (n = 0) Then Return False
        If (n = 1) Then Return False
        For i As Integer = 2 To CInt(Math.Sqrt(n))
            If (n Mod i = 0) Then Return False
        Next
 
        Return True
    End Function
    
    Public Shared Sub Main()
        Dim arr() As Integer = {4, 8, 5, 17, 9, 22, 21, 13, 18, 99}
        Dim sum As Integer = 0

        For i As Integer = 0 To arr.Length - 1
            If isPrime(arr(i)) Then ' 5 17 13
                sum += arr(i) 
            End If
        Next
        
        Console.WriteLine(sum)
    End Sub
End Class



' run:
' 
' 35

 



answered Oct 19, 2019 by avibootz

Related questions

1 answer 145 views
1 answer 91 views
1 answer 105 views
1 answer 89 views
...