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
1
Expert's answer
2012-12-04T09:19:44-0500
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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS