|
-
Oct 28th, 2006, 05:26 PM
#1
Thread Starter
Junior Member
[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.
-
Oct 28th, 2006, 06:53 PM
#2
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
-
Oct 28th, 2006, 07:45 PM
#3
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|