Write a program in VB.net to find the largest prime among a collection of numbers

Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 122
Let's say that we have a collection of integers as {104723, 104729, 9998,448 }. Objective is to find the largest prime among a collection of numbers. The below program will do so

Imports System.Collections.Generic
Imports System.Linq

Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())
Dim numberCollection = New List(Of Integer)() From { _
104723, _
104729, _
9998, _
448 _
Dim nonPrimeCollection = New List(Of Integer)()

For j As Integer = 0 To numberCollection.Count - 1
For i As Integer = 2 To numberCollection(j) - 1
If numberCollection(j) Mod i = 0 Then
Exit For
End If
Console.WriteLine("Largest Prime {0} ", numberCollection.Except(nonPrimeCollection).Max())
End Sub
End Class
End Namespace

Largest Prime 104729

The outer for loop is looping thru the entire collection. The inner for loop starts at 2 and picks up those elements that are not primes. And then we are finding the numbers which are prime and finally finding the max.

Comments or Responses

Login to post response