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,923 questions

51,856 answers

573 users

How to fill a matrix with prime numbers in VB.NET

1 Answer

0 votes
Imports System
 
Public Class Program
    Friend Shared Function isPrime(ByVal num As Integer) As Boolean
        For i As Integer = 2 To num / 2
            If num Mod i = 0 Then
                Return False
            End If
        Next
 
        Return True
    End Function
 
    Public Shared Sub fill_matrix_with_prime_number(matrix(, ) As Integer)
        Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)
        Dim total As Integer = rows * cols
        Dim result As Integer() = New Integer(total - 1) {}
        Dim index As Integer = 0
        Dim num As Integer = 2
 
        While index < total
            If isPrime(num) = True Then
                result(index) = num
                index += 1
            End If
 
            num += 1
        End While
 
        index = 0
 
        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                matrix(i, j) = result(index)
                index += 1
            Next
        Next
    End Sub
 
    Public Shared Sub Main(ByVal args As String())
        Dim rows As Integer = 4, cols As Integer = 5
        Dim matrix(rows - 1, cols - 1) As Integer
 
        fill_matrix_with_prime_number(matrix)
 
        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                Console.Write("{0,3:D}", matrix(i, j))
            Next
            Console.WriteLine()
        Next
    End Sub
End Class
 
 
 
' run:
'
'  2  3  5  7 11
' 13 17 19 23 29
' 31 37 41 43 47
' 53 59 61 67 71
'

 



answered Feb 16, 2024 by avibootz
edited Feb 16, 2024 by avibootz

Related questions

1 answer 59 views
1 answer 158 views
158 views asked Feb 17, 2024 by avibootz
1 answer 114 views
1 answer 94 views
1 answer 132 views
1 answer 139 views
1 answer 118 views
...