Factorial Calculator

Need to take the number from a textbox and get the factorial.

Page 1 of 1

13 Replies - 14863 Views - Last Post: 21 April 2009 - 02:30 PM Rate Topic: -----

#1 Kgodwin88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 18-April 09

Factorial Calculator

Post icon  Posted 18 April 2009 - 02:53 PM

Need to take the number from a textbox and get the factorial

Public Class Form1
	Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
		Dim Number As Integer = txtNumber.Text
		Dim Factorial As Integer
		For i = 1 To Number
			Factorial = i * (i - 1)

		Next
	End Sub

	Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()

	End Sub
End Class



Moderator Note: Please, use code tags when posting code: :code: Also, please post the question in the message body rather then in the topic description.
- Core


Is This A Good Question/Topic? 0
  • +

Replies To: Factorial Calculator

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Factorial Calculator

Posted 18 April 2009 - 03:52 PM

Put code between CODE tags. See CODE button on post editor. Do you have a question about your program?

This post has been edited by June7: 18 April 2009 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Factorial Calculator

Posted 18 April 2009 - 05:00 PM

This is vb.net.
Was This Post Helpful? 0
  • +
  • -

#4 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Factorial Calculator

Posted 18 April 2009 - 05:12 PM

Okay, I think I know what your question would be and might get answer at:
http://msdn.microsof...y/81tad23s.aspx
At least it gave me some new insight.

This post has been edited by June7: 18 April 2009 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 Kgodwin88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 18-April 09

Re: Factorial Calculator

Posted 18 April 2009 - 06:02 PM

I cant seem to get the factorial of the number in the Textbox with the code that I have.
Was This Post Helpful? 0
  • +
  • -

#6 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Factorial Calculator

Posted 18 April 2009 - 06:15 PM

So did you check out the link I posted above? You really need to read it and try some new code then see if you need help. I think that site will clarify a lot.
Was This Post Helpful? 0
  • +
  • -

#7 Nikhil_07n  Icon User is offline

  • The cheese stands alone..
  • member icon

Reputation: 49
  • View blog
  • Posts: 2,489
  • Joined: 09-January 09

Re: Factorial Calculator

Posted 18 April 2009 - 06:59 PM

Here's the code..

Dim x as integer
Dim Fac as Integer
x=Inputbox("Enter a number")
Fac=1
For i=x to 1 Step -1
Fac=Fac * i
Next i
Text1.Text = Fac


Was This Post Helpful? 1
  • +
  • -

#8 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Factorial Calculator

Posted 19 April 2009 - 02:29 AM

Lovely!
That link still has good info about using recursive procedures. It uses the Factorial operation as an example.
Was This Post Helpful? 0
  • +
  • -

#9 Kgodwin88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 18-April 09

Re: Factorial Calculator

Posted 20 April 2009 - 01:32 PM

That code was helpful but i was only able to get teh factorial for numbers 1 through 13 and i need to get the factorial for numbers up to 20

This post has been edited by Kgodwin88: 20 April 2009 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 Kgodwin88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 18-April 09

Re: Factorial Calculator

Posted 20 April 2009 - 02:06 PM

I figured out how to get the factorials of the larger numbers. Now i need to get the answer to display with comas.
Was This Post Helpful? 0
  • +
  • -

#11 LemonMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 170
  • Joined: 22-August 05

Re: Factorial Calculator

Posted 21 April 2009 - 08:25 AM

To add commas:

Dim intNumber As Integer = [NUMBER HERE]
Dim strRep As String = StrReverse(CStr(intNumber))

Dim strResult As String = ""
Dim intCounter As Integer = 0

For intPos As Integer = 0 To (strRep.Length - 1)
strResult &= strRep(intPos)
intCounter += 1
If intCounter = 3 Then
strResult &= ","
intCounter = 0
End If
Next

strResult = StrReverse(strResult)

'The result is contained within strResult


Was This Post Helpful? 0
  • +
  • -

#12 SoloFrog  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 20-April 09

Re: Factorial Calculator

Posted 21 April 2009 - 08:28 AM

His problem i think in his code was that he used Number/factorial as Variable names, can you do that?

This post has been edited by SoloFrog: 21 April 2009 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#13 LemonMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 170
  • Joined: 22-August 05

Re: Factorial Calculator

Posted 21 April 2009 - 08:35 AM

And if you still need some clean factorial code:

Dim intNumber As Integer = [NUMBER HERE]
Dim intResult As Long = 1

For intTerm As Integer = intNumber To 0 Step -1
If intTerm = 0 Then Exit For
intResult = intResult * intTerm
Next

'The result is stored in intResult.



And yes, Factorial and Number are not reserved VB.Nets and are fine to use as variable names or for functions, control names, etc...
Was This Post Helpful? 0
  • +
  • -

#14 SoloFrog  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 20-April 09

Re: Factorial Calculator

Posted 21 April 2009 - 02:30 PM

Public Class Form1
	Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
		Dim Number As Integer = txtNumber.Text
		Dim Factorial As Integer



I dont think if you capatilize them and dont have int before them they wont work. Are you sure?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1