# Factorial Calculator - N! being displayed as 1?

• (2 Pages)
• 1
• 2

## 23 Replies - 2431 Views - Last Post: 17 February 2013 - 08:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312008&amp;s=cf80d7bae27b911ad4bdafc565fd8ff9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aaronenetic

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 12-February 13

# Factorial Calculator - N! being displayed as 1?

Posted 12 February 2013 - 07:55 AM

Hey guys, I've been working on an assignment creating a factorial calculator with the psuedo code given. I have followed the following comments and psuedo code given (Basically a factorial calculator using different data types, by clicking different buttons ex. Byte button, short button etc.):

```‘Declare static variables MK_n and MK_factorial as Byte.
‘Display the label “Byte” under the label “Data Type”
MK_n = MK_n + 1
If (MK_n = 1) then
MK_factorial = 1
Else
MK_factorial = MK_factorial * MK_n
End If
‘Display the values of MK_n and MK_factorial in the corresponding text boxes.
```

I have come up with this code:

```
Private Sub AH_Button_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AH_Button_1.Click
Dim AH_n As Byte
Dim AH_factorial As Byte
Dim AH_Textbox_1 As Byte

AH_Label_5.Text = "Byte"
AH_n = AH_n + 1
If AH_n = 1 Then
AH_factorial = 1
Else
AH_factorial = AH_factorial * AH_n
End If
AH_TextBox_2.Text = AH_factorial

End Sub
```

AH_TextBox1 is supposed to be the N value input by the user, while AH_TextBox2 is supposed to show the final factorial value.

The problem I'm having is that the value of AH_factorial (TextBox2) keeps returning 1 no matter what the N value is.

Can anyone point out the flaw in my code?

Thanks.
Aaron

Is This A Good Question/Topic? 0

## Replies To: Factorial Calculator - N! being displayed as 1?

### #2 AdamSpeight2008

• MrCupOfT

Reputation: 2290
• Posts: 9,528
• Joined: 29-May 08

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

Posted 12 February 2013 - 08:06 AM

Suggest you use the debugger to step through the code, does it do actually what you think it does.
Was This Post Helpful? 0

### #3 aaronenetic

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 12-February 13

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

Posted 12 February 2013 - 08:24 AM

I did that, it's not showing me anything. No luck.
Was This Post Helpful? 0

### #4 AdamSpeight2008

• MrCupOfT

Reputation: 2290
• Posts: 9,528
• Joined: 29-May 08

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

Posted 12 February 2013 - 08:28 AM

Really. What path through the code does it take?

What algorithm are you using? Does your program execution follow it?
Was This Post Helpful? 0

### #5 Razakel

• New D.I.C Head

Reputation: 3
• Posts: 5
• Joined: 05-February 10

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

Posted 12 February 2013 - 08:37 AM

Does the Else clause ever get executed? As it currently stands you don't seem to be taking in any sort of user input, so whatever AH_n is declared as is what the code outputs. It may just be going to one because that's what you tell it to do...
Was This Post Helpful? 0

### #6 aaronenetic

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 12-February 13

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

Posted 12 February 2013 - 09:41 AM

I edited the the code so that it would take the user input. But I realize it's not getting to the else statement, meaning the value of AH_n is 1. It's supposed to be the value of the text box..
``` Private Sub AH_Button_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AH_Button_1.Click
Dim AH_n As Byte
Dim AH_factorial As Byte
Dim AH_Textbox_1 As Byte

AH_Label_5.Text = "Byte"
AH_n = AH_Textbox_1 + 1
If AH_n = 1 Then
AH_factorial = 10
Else
AH_factorial = AH_factorial * AH_n
End If
AH_TextBox_2.Text = AH_factorial

End Sub

```

Was This Post Helpful? 0

### #7 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,294
• Joined: 12-September 12

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

Posted 12 February 2013 - 10:00 AM

aaronenetic, on 12 February 2013 - 10:41 AM, said:

I edited the the code so that it would take the user input. But I realize it's not getting to the else statement, meaning the value of AH_n is 1. It's supposed to be the value of the text box..

Again, debug your code by single-stepping through it. If you are not sure how to do this, have a look at the Debugging Skills Tutorial.

A couple of things you might want to look at in your code.

If you WANT to use the value in the TextBox, then USE the value in the text box.
If you are just making random changes to your code, you will not be successful, and you will not learn anything. For example, you changed a line that said AH_factorial = 1 to AH_factorial = 10. Why did you do that?

It looks like you are trying to find the factorial by using recursion (calling a function from within itself), becuase you do not have a loop in there. If this is the case, be aware that you will need to write a separate function that is called with the value required. If you do not want to use recursion, you will need a loop.

My advice to you is, above all else, to figure out how to calculate a factorial of a number one step at a time, and to try to think of how you will tell the computer to do that.
Was This Post Helpful? 0

### #8 darkrider105

• New D.I.C Head

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

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

Posted 12 February 2013 - 02:17 PM

The value is supposed to increased by 1 every time you click the button and then factored by that amount.
Was This Post Helpful? 0

### #9 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,294
• Joined: 12-September 12

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

Posted 12 February 2013 - 03:14 PM

darkrider105, on 12 February 2013 - 03:17 PM, said:

The value is supposed to increased by 1 every time you click the button and then factored by that amount.

Do you know this for sure? Are you acquainted with aaronenetic and his project?
His code most assuredly does NOT increase the the value in the TextBox.

aaronenetic needs to learn a few fundamentals before jumping in to writing a function like this.
Was This Post Helpful? 0

### #10 darkrider105

• New D.I.C Head

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

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

Posted 12 February 2013 - 03:36 PM

If you're still having trouble know that you must declare you variables as global

Do you know this for sure? Are you acquainted with aaronenetic and his project?
His code most assuredly does NOT increase the the value in the TextBox.

aaronenetic needs to learn a few fundamentals before jumping in to writing a function like this.
[/quote]
He doing the same assignment as me at least it seems that way from the initial code which uses our professors initials
Was This Post Helpful? 0

### #11 darkrider105

• New D.I.C Head

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

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

Posted 12 February 2013 - 03:44 PM

Nm the previous statement of global it does not work for the rest of the buttons
Was This Post Helpful? 0

### #12 darkrider105

• New D.I.C Head

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

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

Posted 12 February 2013 - 05:34 PM

```    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"
Dim CN_N As Short
Dim CN_factorial As Short

CN_N = (factorial())

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

```

Okay I have been having trouble getting this to repeat. Like the first poster the end result keeps coming up as 1 and 1. I essentially want CN_N to increase by 1 every time they user clicks. Any thoughts on what I need to change at this point?
Was This Post Helpful? 0

### #13 AdamSpeight2008

• MrCupOfT

Reputation: 2290
• Posts: 9,528
• Joined: 29-May 08

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

Posted 12 February 2013 - 06:42 PM

I think you need to go make to basic and Create a new console application project.
So now is just the basics
- No use input
- No text box
- No Controls
Write a simple function that calculate factorial function.
```Public Module Module1

Public Sub Main()

Dim f1 = Factorial(3)

End Sub

Function Factorial(n As ) As

Return
End Function

End Module

```

Was This Post Helpful? 0

### #14 darkrider105

• New D.I.C Head

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

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

Posted 12 February 2013 - 07:41 PM

``` Public Module Module1

Public Sub Main()
Dim f1 = factorial(3)

End Sub
Function factorial(ByVal n As Double) As Double
Return n = n * (n - 1)
End Function

End Module

```

I barely know how to use windows form and so i don't really know what I am doing even in this.
Really sorry
Was This Post Helpful? 0

### #15 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,294
• Joined: 12-September 12

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

Posted 12 February 2013 - 09:16 PM

darkrider105, on 12 February 2013 - 08:41 PM, said:

``` Public Module Module1

Public Sub Main()
Dim f1 = factorial(3)

End Sub
Function factorial(ByVal n As Double) As Double
Return n = n * (n - 1)
End Function

End Module

```

I barely know how to use windows form and so i don't really know what I am doing even in this.
Really sorry

Think about what you are trying to do. Think in discrete, small steps. The code you just posted will indeed return the right answer, but it will not return the right answer for any input larger than 3.

What this function is saying is "return the value of the input multiplied by the input -1, so it will return 20 for an input of 5, instead of 120.

Are you reqired to perform the calculation with a loop, or with recursion, or can you decide this for yourself?
Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }