Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,539 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,785 people online right now. Registration is fast and FREE... Join Now!




Factorial From Textbox

 
Reply to this topicStart new topic

Factorial From Textbox

chris bell
20 Aug, 2008 - 04:07 AM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2008
Posts: 2

Im looking to get the factorial value of an integer betweeen 1 and 15 (thats the nly numbers to be allowed to be inserted into the textbox).

I cannot for the life of me figure it out. Here is what i have so far:

CODE
'
' Created by SharpDevelop.
' User: 07700988
' Date: 20/08/2008
' Time: 11:55
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Public Partial Class MainForm
    Public Sub New()
        ' The Me.InitializeComponent call is required for Windows Forms designer support.
        Me.InitializeComponent()
        
        '
        ' TODO : Add constructor code after InitializeComponents
        '
    End Sub
    
    Private Sub factorials(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim intInputNbr As Integer = 15
        Dim intLoopCtr As Integer
        Dim numCtr As Integer = 1
        Dim lngFactorial As Long = 1
    
        For intLoopCtr = 1 to intInputNbr
            lngFactorial = lngFactorial * intLoopCtr
            lblNumberSeries.Text= numCtr & ControlChars.CrLf
            lblNumberFactorial.text += lngFactorial & ControlChars.CrLf
            numctr += 1
            Console.WriteLine(lngFactorial)
        Next
    End Sub

End Class


Thanks in Advance

Oh yea it is also jsut outputted to a label atm with the value being automatically 15
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Factorial From Textbox
20 Aug, 2008 - 08:49 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET
User is online!Profile CardPM
+Quote Post

djkitt
RE: Factorial From Textbox
20 Aug, 2008 - 09:21 AM
Post #3

D.I.C Head
**

Joined: 22 May, 2008
Posts: 128



Thanked: 13 times
My Contributions
So, I am not sure what you are asking.

Your code *looks* like it is being executed when the form is loaded and only when the form is loaded.

Is that the behavior you are looking for?

Are you asking how to make this code work based on a number input into a textbox?

If so, just add a button and move you code to the button_click event. Then in your code assign the value of txtYourTextBox.Text to intInpuNbr.

If none of this really addresses your problem please let me know.

Hope this helps,

Kitt

This post has been edited by djkitt: 20 Aug, 2008 - 09:22 AM
User is offlineProfile CardPM
+Quote Post

nofear217
RE: Factorial From Textbox
20 Aug, 2008 - 09:38 AM
Post #4

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 164



Thanked: 1 times
Dream Kudos: 175
My Contributions
Easiest way to do factorials is through recursion...I've added a code snippet which can be found here. It takes the number for which you want to get the factorial and then returns the factorial value. I used double because factorials have a tendency to grow extremely quickly.

So your code would look something like this:

vb

Private Function Factorial(ByVal number As Double) As Double

If number <= 1 Then
Return (1)
Else
Return number * Factorial(number - 1)
End If

End Function

Private Sub factorials(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim douFactorial as Double = Factorial(15)
lblNumberFactorial.Text = douFactorial.ToString
End Sub


If you wanted to output the value each time the function recurses through...just add the line underneath the
CODE

Return number * Factorial(number - 1)


This post has been edited by nofear217: 20 Aug, 2008 - 09:43 AM
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Factorial From Textbox
20 Aug, 2008 - 01:43 PM
Post #5

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
QUOTE(nofear217 @ 20 Aug, 2008 - 06:38 PM) *

Easiest way to do factorials is through recursion...


With a recursive routine you run the risk of a stack overflow, and it not hard to implement to iteratively.
vb

Public Function Factorial(Byval number as integer) as double
Factorial=1
While number>0
Factorial *=number
number-=1
End While
End Function


This post has been edited by AdamSpeight2008: 20 Aug, 2008 - 02:17 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:31PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month