# Answer to Question #19887 in Visual Basic for Annie

Question #19887

write to program to input that the ending no from the user and display all the prime no according to this number

Expert's answer

Module Module1

Sub Main()

Dim n As Integer = 0

Console.Write("Please enter a number larger than two: ")

n = Integer.Parse(Console.ReadLine())

For i As Integer = 2 To n

Dim prime As Boolean = IsPrime(i)

If prime Then

Console.Write(i.ToString() + " Prime" + Environment.NewLine())

End If

Next

Console.Write("Press any key to exit...")

Console.ReadLine()

End Sub

Private Function IsPrime(ByVal number As Integer) As Boolean

If ((number And 1) = 0) Then

If (number = 2) Then

Return True

Else

Return False

End If

End If

For i As Integer = 3 To number Step 2

i *= i

If ((number Mod i) = 0) Then

Return False

End If

Next

Return number <> 1

End Function

End Module

Sub Main()

Dim n As Integer = 0

Console.Write("Please enter a number larger than two: ")

n = Integer.Parse(Console.ReadLine())

For i As Integer = 2 To n

Dim prime As Boolean = IsPrime(i)

If prime Then

Console.Write(i.ToString() + " Prime" + Environment.NewLine())

End If

Next

Console.Write("Press any key to exit...")

Console.ReadLine()

End Sub

Private Function IsPrime(ByVal number As Integer) As Boolean

If ((number And 1) = 0) Then

If (number = 2) Then

Return True

Else

Return False

End If

End If

For i As Integer = 3 To number Step 2

i *= i

If ((number Mod i) = 0) Then

Return False

End If

Next

Return number <> 1

End Function

End Module

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment