By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,594 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,594 IT Pros & Developers. It's quick & easy.

Calculate the factorial

P: 1
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
Share this Question
Share on Google+
1 Reply


100+
P: 1,646
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.