Check whether an inputted number is prime?
Hi. I'm working on a console application in VB .net that checks a number that the user has inputted, and tells you whether the number is prime.
The number that has to be inputted has to be between 1 and 100. I have all the code so far and at the moment it can check whether the number is or isn't within the boundries.
However at the moment I'm not quite sure what the line of code would be to check whether the number is prime or not. Please could you help?
Thanks a lot. :thumb:
Re: Check whether an inputted number is prime?
Re: Check whether an inputted number is prime?
Well it depends on the point of the exercise. This sounds kind of like a homework assignment, so they might want you to do some math to figure out if the number is prime. One other way to do it would be to declare an array of all primes under 100 and see if the number is in the list:
Code:
Dim primes() As Integer = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim somenumber As Integer = Integer.Parse(textbox1.text)
If primes.Contains(somenumber) Then
MessageBox.Show("Prime")
End If
End Sub
Re: Check whether an inputted number is prime?
I was going to say put all the prime numbers in an array and see it if contains that number, but -0 beat me to it. :)
I was just looking through the Google results and I found this:
http://www.dotnetspider.com/resource...eneration.aspx
Re: Check whether an inputted number is prime?
I agree that the array way would be easy, but doesn't really teach him to solve the problem of "determining if a number is prime."
if he ever wanted to extend his program to numbers 1-1000, then he'd be back here posting the same question again.