Factorial Calculator - N! being displayed as 1?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2010 Views - Last Post: 17 February 2013 - 08:13 AM Rate Topic: -----

#16 darkrider105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-February 13

Re: Factorial Calculator - N! being displayed as 1?

Posted 13 February 2013 - 12:53 PM

It doesn't matter if I use recursion or a loop as long as each time you click a button n increases by 1
Was This Post Helpful? 0
  • +
  • -

#17 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Factorial Calculator - N! being displayed as 1?

Posted 13 February 2013 - 02:26 PM

View Postdarkrider105, on 13 February 2013 - 01:53 PM, said:

It doesn't matter if I use recursion or a loop as long as each time you click a button n increases by 1


Pseudo-Code:
Assuming that InputTextBox has a value 1 less than the value you want to calculate first:
    Sub Button Click event
      Increment value in InputTextBox
      Call factorial function with value in InputTextBox
      Display returned value in OutputTextBox
    End Sub

    Function Factorial(n)
      Calculate factorial
      Return Factorial
    End Function


Notice that we are separating the button event from the code that finds the factorial.
So how would you code that? Work it out a small piece at a time.
Was This Post Helpful? 0
  • +
  • -

#18 darkrider105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-February 13

Re: Factorial Calculator - N! being displayed as 1?

Posted 14 February 2013 - 01:15 PM

I figured it out in some round about way but thanks
Was This Post Helpful? 0
  • +
  • -

#19 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,479
  • Joined: 29-May 08

Re: Factorial Calculator - N! being displayed as 1?

Posted 14 February 2013 - 01:25 PM

Care to share with the DIC community?
Was This Post Helpful? 0
  • +
  • -

#20 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Factorial Calculator - N! being displayed as 1?

Posted 14 February 2013 - 04:50 PM

View Postdarkrider105, on 14 February 2013 - 02:15 PM, said:

I figured it out in some round about way but thanks

If you're here just to get this particular job done, fine, but if you're here to learn (an assumption I almost always make), then we'd love to see how you did it. This will allow others to see the process, and allow us to (perhaps) point you toward some other techniques.
Was This Post Helpful? 0
  • +
  • -

#21 darkrider105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-February 13

Re: Factorial Calculator - N! being displayed as 1?

Posted 15 February 2013 - 06:56 PM

I'll post it
 Public Class CN_frm_Assig_3

    Private CN_N As Byte
    Private CN_factorial As Byte
    Private CN_factorial2 As Short
    Private CN_N2 As Short
    Private CN_factorial3 As Integer
    Private CN_N3 As Integer
    Private CN_factorial4 As Long
    Private CN_N4 As Long
    Private CN_factorial5 As Single
    Private CN_N5 As Single
    Private CN_factorial6 As Double
    Private CN_N6 As Double

    Private Sub CN_frm_Assig_3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_lbl_factorial.Click

    End Sub
    

    Private Sub CN_btn_Byte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_btn_Byte.Click
        CN_lbl_Type.Text = "Byte"



        CN_N = CN_N + 1
        If (CN_N = 1) Then

            CN_factorial = 1
        Else
            CN_factorial = CN_factorial * CN_N

        End If

        CN_txt_N.Text = CN_N

        CN_txt_factorial.Text = CN_factorial
      



    End Sub



    Private Sub CN_btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_btn_Exit.Click
        Me.Close()
        Me.Dispose()

    End Sub

    Private Sub CN_txt_N_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_txt_N.TextChanged

    End Sub

    Private Sub CN_txt_factorial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_txt_factorial.TextChanged

    End Sub


    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Private Function factorial() As Short

        If CN_N = 1 Then
            Return 1
            Return CN_factorial * CN_N

        End If
    End Function


    Private Sub CN_btn_short_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_btn_short.Click
        CN_lbl_Type.Text = "Short"
        CN_N2 = CN_N2 + 1
        If (CN_N2 = 1) Then
            CN_factorial2 = 1
        Else
            CN_factorial2 = CN_factorial2 * CN_N2
        End If
        CN_txt_N.Text = CN_N2
        CN_txt_factorial.Text = CN_factorial2
    End Sub

    Private Sub CN_btn_Int_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_btn_Int.Click
        CN_lbl_Type.Text = "Integer"

        CN_N3 = CN_N3 + 1
        If (CN_N3 = 1) Then
            CN_factorial3 = 1
        Else
            Do
                CN_factorial3 = CN_factorial3 * CN_N3
            Loop

        End If
        
        CN_txt_N.Text = CN_N3
        CN_txt_factorial.Text = CN_factorial3
    End Sub

    Private Sub CN_btn_long_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_btn_long.Click
        CN_lbl_Type.Text = "Long"
        CN_N4 = CN_N4 + 1
        If (CN_N4 = 1) Then
            CN_factorial4 = 1
        Else
            CN_factorial4 = CN_factorial4 * CN_N4
        End If
        CN_txt_N.Text = CN_N4
        CN_txt_factorial.Text = CN_factorial4
    End Sub

    Private Sub CN_btn_single_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_btn_single.Click
        CN_lbl_Type.Text = "Single"
        CN_N5 = CN_N5 + 1
        If (CN_N5 = 1) Then
            CN_factorial5 = 1
        Else
            CN_factorial5 = CN_factorial5 * CN_N5
        End If
        CN_txt_N.Text = CN_N5
        CN_txt_factorial.Text = CN_factorial5
    End Sub

    Private Sub CN_btn_double_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CN_btn_double.Click
        CN_lbl_Type.Text = "Double"
        CN_N6 = CN_N6 + 1
        If (CN_N6 = 1) Then
            CN_factorial6 = 1
        Else
            CN_factorial6 = CN_factorial6 * CN_N6
        End If
        CN_txt_N.Text = CN_N6
        CN_txt_factorial.Text = CN_factorial6
    End Sub
End Class


it does what it's supposed to do but like i said its in a weird round about way
Thanks to everyone for the help
Was This Post Helpful? 0
  • +
  • -

#22 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Factorial Calculator - N! being displayed as 1?

Posted 15 February 2013 - 09:35 PM

View Postdarkrider105, on 15 February 2013 - 07:56 PM, said:

it does what it's supposed to do but like i said its in a weird round about way
Thanks to everyone for the help

What you are doing is not really a factorial. It seems like it is because it arrives at the right answers, but it is doing it only because you keep the answer from the previous operation. If, for example, you started with an input of 4, it would not arrive at the right answer, because you have not calculated the factorial of 3. 3 is a special case, and does not actually need the factorial of 2 in order to be correct (any factorial does not need the 1 as a multiplier).

Frankly, if I was your professor, I'd probably compliment you on finding a way to do it without actually writing a factorial routine, but would have you redo it.

So, if you're interested, we can work on this code and make it do a real factorial with any number as input, and which does not depend on a previous answer, at which time it becomes trivial to supply the factorial function with incrementing inputs. It isn't overly difficult, so let us know.
Was This Post Helpful? 0
  • +
  • -

#23 darkrider105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-February 13

Re: Factorial Calculator - N! being displayed as 1?

Posted 16 February 2013 - 11:06 PM

Well I didn't get to hand it in on time so I she never gave me input on this. I still wanted to try. the assignment had asked for it to just increase as you click and not just start off randomly. At the moment I'm happy with this .
I do have another assignment so I may need help figuring that out after a few hours trying to understand.
Thanks for the help anyway
Was This Post Helpful? 0
  • +
  • -

#24 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Factorial Calculator - N! being displayed as 1?

Posted 17 February 2013 - 08:13 AM

View Postdarkrider105, on 17 February 2013 - 12:06 AM, said:

Well I didn't get to hand it in on time so I she never gave me input on this. I still wanted to try. the assignment had asked for it to just increase as you click and not just start off randomly. At the moment I'm happy with this .

OK, but I think I'll just post this little bit of code to show you how simple a factorial function can be. This particular one uses recursion. When you call it, it will return with the factorial of any number, provided the factorial fits into the data type (Integer in this case). It would need some error checking to prevent an Overflow Exception in order to be robust.

    Private Function factorial(ByVal n As Integer) As Integer
        If n = 1 Then
            Return n * 1
        Else
            Return n * factorial(n - 1)
        End If
    End Function


A non-recursive factorial function is easier to understand, but I'll leave that out for now.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2