• 0

vb.net factorial


Question

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
Share on other sites

7 answers to this question

Recommended Posts

  • 0
   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
Share on other sites

  • 0

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
Share on other sites

  • 0
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
Share on other sites

  • 0
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
Share on other sites

  • 0
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
Share on other sites

  • 0

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
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.