Results 1 to 3 of 3

Thread: [RESOLVED] [02/03] Factorial Algorithm

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Resolved [RESOLVED] [02/03] Factorial Algorithm

    With Option Strict On, I'm trying to do a simple loop that will calculate the factorial of a number sent from an input box. The calculated factorial should be displayed in a list box. This is my code so far and it does NOT work:

    Dim factorialInput As String
    Dim n As Double = Val(factorialInput)
    Dim total As Double = 1
    factorialInput = InputBox("Please enter an integer greater than or equal to zero.", "Provide a Value")


    If n > 0 Then
    For n = 1 To n
    total = total * n
    Next n
    lstWorkbench.Items.Add(factorialInput & "! = " & total)
    End If

    If n < 0 Then
    factorialInput = InputBox("Please enter an integer greater than or equal to zero.", "Provide a Value")
    End If



    The result from the code is blank.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [02/03] Factorial Algorithm

    Because you are calling Val() on the factorialInput variable before you even get a value for it from the inputbox, so N I would assume would be 0, which then just skips your loop since it is not greater than 0. You have to convert the input from the InputBox to a number after the user enters it

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Talking Re: [02/03] Factorial Algorithm

    OMGOSH YOU DID IT! Thank you! I can't believe that was the problem and it was very simple and I didn't even see it. But hey. At least I got the hard part right. Thank you very much.

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