Chan Smith Posted October 20, 2004 Share Posted October 20, 2004 I need to write a programing in visual basic .net to calculate the factorial of a number using a do loop. Anyone know how? a factorial is 4 * 3 * 2 * 1 = 24 factorial of 4 Link to comment https://www.neowin.net/forum/topic/233498-vbnet-factorial/ Share on other sites More sharing options...
0 Winston Posted October 21, 2004 Share Posted October 21, 2004 Function calculateFactorial(ByVal number As Integer) As Double Dim factorial As Double = 1.0 For i As Double = 1.0 To number factorial = factorial * i Next Return factorial End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show(Me.calculateFactorial(4)) End Sub Link to comment https://www.neowin.net/forum/topic/233498-vbnet-factorial/#findComment-584778582 Share on other sites More sharing options...
0 John Veteran Posted October 21, 2004 Veteran Share Posted October 21, 2004 You don't need to use doubles :huh: Integers work fine; if you're using large numbers, use longs (System.Int64, I forget what VB calls them) (Y) But otherwise, Winston's code is right :yes: Link to comment https://www.neowin.net/forum/topic/233498-vbnet-factorial/#findComment-584779593 Share on other sites More sharing options...
0 Winston Posted October 21, 2004 Share Posted October 21, 2004 gameguy said: You don't need to use doubles :huh: Integers work fine; if you're using large numbers, use longs (System.Int64, I forget what VB calls them) (Y)But otherwise, Winston's code is right :yes: 584779593[/snapback] I wasn't too sure, i had to use doubles, i tested it out inside the normal windows calc and put in say 90!, and it gave a double back, so i just used double and it works fine, so meh :) Link to comment https://www.neowin.net/forum/topic/233498-vbnet-factorial/#findComment-584781686 Share on other sites More sharing options...
0 John Veteran Posted October 22, 2004 Veteran Share Posted October 22, 2004 Winston said: I wasn't too sure, i had to use doubles, i tested it out inside the normal windows calc and put in say 90!, and it gave a double back, so i just used double and it works fine, so meh :) 584781686[/snapback] Well the factorial of 90 is an absolutely enormous number, so what do you expect? :p Link to comment https://www.neowin.net/forum/topic/233498-vbnet-factorial/#findComment-584788182 Share on other sites More sharing options...
0 snippet1 Posted October 22, 2004 Share Posted October 22, 2004 gameguy said: Well the factorial of 90 is an absolutely enormous number, so what do you expect? :p 584788182[/snapback] But we hvae to consider strangely large numbers for the user! Link to comment https://www.neowin.net/forum/topic/233498-vbnet-factorial/#findComment-584788194 Share on other sites More sharing options...
0 John Veteran Posted October 23, 2004 Veteran Share Posted October 23, 2004 That's a job better left for validation :yes: Link to comment https://www.neowin.net/forum/topic/233498-vbnet-factorial/#findComment-584788319 Share on other sites More sharing options...
0 Sn1p3t Posted October 24, 2004 Share Posted October 24, 2004 Use Longs instead. Factorials are always going to be integer values, but I think Longs can hold larger values than Doubles (and of course I'm having problems finding that info on Google :p) Link to comment https://www.neowin.net/forum/topic/233498-vbnet-factorial/#findComment-584796626 Share on other sites More sharing options...
Question
Chan Smith
I need to write a programing in visual basic .net to calculate the factorial of a number using a do loop. Anyone know how?
a factorial is 4 * 3 * 2 * 1 = 24
factorial of 4
Link to comment
https://www.neowin.net/forum/topic/233498-vbnet-factorial/Share on other sites
7 answers to this question
Recommended Posts