Results 1 to 5 of 5

Thread: Check whether an inputted number is prime?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    26

    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.

  2. #2

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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

  4. #4
    Hyperactive Member BadgerBadger's Avatar
    Join Date
    Aug 2009
    Location
    Wales
    Posts
    382

    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

  5. #5
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width