467,151 Members | 900 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,151 developers. It's quick & easy.

Calculate the factorial

Dear Sir/Madam,

I have one assignment , which need me to

write a program to calculate the factorial of an integer,

then press the button that would display the result in a label.3

& the program should warn the user if the

iinput is not a number.

I have tried my best to write ,but It show me

something wrong,

What I write is as following, Please help me to show what is going wrong !
Thanks !

Tin



Dim N As Integer = CInt(TextBox1.Text)
Dim sum As Integer
Dim Factorial As Integer

Try

N = Integer.Parse(TextBox1.Text)

sum = N * Factorial(N - 1)

Label3.Text = CStr(sum)

Catch ex As FormatException
MessageBox.Show("Number is not an integer. Please enter an Integer ")


End Try

End Sub



Function Factorial(ByVal N As Integer) As Integer
If N = 0 Then
Return 1
Else
Return N * Factoria((N - 1) - 1)
End If
End Function
Nov 9 '06 #1
  • viewed: 8240
Share:
1 Reply
1GB
Dear Sir/Madam,

I have one assignment , which need me to

write a program to calculate the factorial of an integer,

then press the button that would display the result in a label.3

& the program should warn the user if the

iinput is not a number.

I have tried my best to write ,but It show me

something wrong,

What I write is as following, Please help me to show what is going wrong !
Thanks !

Tin



Dim N As Integer = CInt(TextBox1.Text)
Dim sum As Integer
Dim Factorial As Integer

Try

N = Integer.Parse(TextBox1.Text)

sum = N * Factorial(N - 1)

Label3.Text = CStr(sum)

Catch ex As FormatException
MessageBox.Show("Number is not an integer. Please enter an Integer ")


End Try

End Sub



Function Factorial(ByVal N As Integer) As Integer
If N = 0 Then
Return 1
Else
Return N * Factoria((N - 1) - 1)
End If
End Function
Hi, a couple of things might help.
The Try...Catch block is C++ programming and is not part of VB6
You need to make sure that the input number is 171 or less or you will get an overflow error as the factorial of 172 is too large to store in a Double.
You have declared an integer with the same name as the function.

The following works

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFactorial_Click()
  2.  Dim dblN As Double
  3.  Dim dblFactorial As Double
  4.  
  5. If IsNumeric(TextBox1.Text) _
  6. And Len(TextBox1.Text) < 4 Then
  7.    dblN = CLng(TextBox1.Text)
  8.    If dblN > 170 Then
  9.       MsgBox "Please enter a numeric value between 1 and 170"
  10.       TextBox1.Text = ""
  11.       TextBox1.SetFocus
  12.       Exit Sub
  13.    End If
  14. Else
  15.    MsgBox "Please enter a numeric value between 1 and 170"
  16.    TextBox1.Text = ""
  17.    TextBox1.SetFocus
  18.    Exit Sub
  19. End If
  20. dblFactorial = Factorial(dblN)
  21. TextBox1.Text = dblFactorial
  22. End Sub
  23.  
  24. Private Function Factorial(dblNum As Double) As Double
  25.    If dblNum - 1 = 0 Then
  26.       Factorial = dblNum
  27.       Exit Function
  28.    End If
  29.    Factorial = dblNum * Factorial(dblNum - 1)
  30. End Function
  31.  
Nov 9 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

33 posts views Thread by patrick_woflian | last post: by
35 posts views Thread by aNt17017 | last post: by
59 posts views Thread by Umesh | last post: by
3 posts views Thread by Sugandh Jain | last post: by
2 posts views Thread by becky808 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.