English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Not working, have all the buttons and labels fine, but the math part is screwing it up....help?


Public Class btnFactorial
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "


Friend WithEvents btnFactorial As Button 'generates output

' Visual Studio .NET generated code

Private Sub btnFactorial_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnFactorial.Click, MyBase.Click
Dim x As Double
Dim Fact As Double

lblOutput.Text = "To determine the Factorial of a number. " _
& "Please enter a number less than 10." _
& "Then, click on the Factorial Button to view the results."

x = Val(txtInput.Text)

Fact = Factorial(x)
lblOutput.Text = ("")
lblOutput.Text = (" " & x & "! is " & Fact)


End Sub ' btnFactorial_Click

' generates factorial of number
Private Function Factorial(ByVal x As Double) As Double


If x <= 1 Then ' base case
Factorial = 1
Else
Factorial = x * Factorial(x - 1)
End If

End Function ' Factorial


#End Region
Friend WithEvents lblOutput As System.Windows.Forms.Button ' prompts for integer
Friend WithEvents txtInput As System.Windows.Forms.TextBox ' reads an integer
Friend WithEvents txtDisplay As System.Windows.Forms.TextBox ' displays output
Friend WithEvents lblFactorial As System.Windows.Forms.Label ' indicates output
Friend WithEvents lblEnter As System.Windows.Forms.Label '

Private Sub InitializeComponent()
Me.lblOutput = New System.Windows.Forms.Button
Me.txtInput = New System.Windows.Forms.TextBox
Me.lblFactorial = New System.Windows.Forms.Label
Me.lblEnter = New System.Windows.Forms.Label
Me.txtDisplay = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'lblOutput
'
Me.lblOutput.BackColor = System.Drawing.SystemColors.Desktop
Me.lblOutput.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblOutput.ForeColor = System.Drawing.SystemColors.ControlLightLight
Me.lblOutput.Location = New System.Drawing.Point(78, 64)
Me.lblOutput.Name = "lblOutput"
Me.lblOutput.Size = New System.Drawing.Size(136, 40)
Me.lblOutput.TabIndex = 0
Me.lblOutput.Text = "Calculate"
'
'txtInput
'
Me.txtInput.Location = New System.Drawing.Point(184, 16)
Me.txtInput.Name = "txtInput"
Me.txtInput.Size = New System.Drawing.Size(80, 20)
Me.txtInput.TabIndex = 1
Me.txtInput.Text = "0"
'
'lblFactorial
'
Me.lblFactorial.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblFactorial.ForeColor = System.Drawing.SystemColors.ControlLightLight
Me.lblFactorial.Location = New System.Drawing.Point(82, 136)
Me.lblFactorial.Name = "lblFactorial"
Me.lblFactorial.Size = New System.Drawing.Size(128, 23)
Me.lblFactorial.TabIndex = 4
Me.lblFactorial.Text = "Factorial"
Me.lblFactorial.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'lblEnter
'
Me.lblEnter.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblEnter.ForeColor = System.Drawing.SystemColors.ControlLightLight
Me.lblEnter.Location = New System.Drawing.Point(8, 8)
Me.lblEnter.Name = "lblEnter"
Me.lblEnter.Size = New System.Drawing.Size(168, 32)
Me.lblEnter.TabIndex = 5
Me.lblEnter.Text = "Enter An Integer:"
Me.lblEnter.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'txtDisplay
'
Me.txtDisplay.Location = New System.Drawing.Point(48, 176)
Me.txtDisplay.Name = "txtDisplay"
Me.txtDisplay.Size = New System.Drawing.Size(200, 20)
Me.txtDisplay.TabIndex = 6
Me.txtDisplay.Text = "0"
'
'btnFactorial
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.Desktop
Me.ClientSize = New System.Drawing.Size(292, 334)
Me.Controls.Add(Me.txtDisplay)
Me.Controls.Add(Me.lblEnter)
Me.Controls.Add(Me.lblFactorial)
Me.Controls.Add(Me.txtInput)
Me.Controls.Add(Me.lblOutput)
Me.Name = "btnFactorial"
Me.ResumeLayout(False)

End Sub
End Class ' btnFactorial

2007-03-08 03:23:03 · 3 answers · asked by bloodyhell1981 1 in Computers & Internet Programming & Design

3 answers

The problem is here:
Else
Factorial = x * Factorial(x - 1)
End If

7! = 7x6x5x4x3x2x1
Your calculation only gives you 7x6
You need to LOOP 'x' to get the rest of the values like:

Factoral = 1

Do until x = 1
Factoral = Factoral * x
x = x-1
LOOP

that should get it..hope that helps

2007-03-08 03:52:14 · answer #1 · answered by rod 6 · 1 0

I tried your program and what I found was the factorial procedure wasn't executed. This is fixed in this way:

below
Me.lblOutput.Text = "Calculate"
add this code:

AddHandler Me.lblOutput.Click, AddressOf Me.btnFactorial_Click

(one single line) This is going to tell the button that on click execute btnFactorial_Click. Maths looks fine... it worked for all numbers I tried..

2007-03-08 04:04:57 · answer #2 · answered by Anonymous · 0 0

This line

Factorial = x * Factorial(x - 1)


Should be part of a Do/While/Loop or Do/Until/Loop construct

Do While X >1
Factorial = x * Factorial(x - 1)
LOOP

or

DO UNTIL X = 1
Factorial = x * Factorial(x - 1)
LOOP

2007-03-08 04:40:08 · answer #3 · answered by Richard H 7 · 0 0

fedest.com, questions and answers