944,168 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 24128
  • VB.NET RSS
Sep 10th, 2004
0

Factorial Calculate

Expand Post »
I want to calculate factorial with all proper validations. Can anyone tell me the functions to be use for this using VB.NET...pLz
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deepsmehta is offline Offline
1 posts
since Sep 2004
Sep 1st, 2008
0

Re: Factorial Calculate

i use procedures

u have to rename some words in these code as it is my code
like (txtfirstnum.text = ur text box ) (txtanswer.text= ur answer box) thats all i think

VALIDATION

vb.net Syntax (Toggle Plain Text)
  1. Public Sub validation()
  2. If txtFirstNum.Text = "" Then
  3. MsgBox("First Number is Empty")
  4. ElseIf Not IsNumeric(txtFirstNum.Text) Then
  5. MsgBox("First Number is not numeric")
  6. Else
  7. call factorial
  8. endif
  9. endsub

FACTORIAL

vb.net Syntax (Toggle Plain Text)
  1. Public Sub factorial()
  2. Dim counter As Integer
  3. Dim result As Long
  4. Dim x As String = (Val(txtFirstNum.Text))
  5. counter = 1
  6. result = 1
  7. While counter <= x
  8. result = result * counter
  9.  
  10. counter = counter + 1
  11.  
  12. txtAnswer.Text = result
  13. End While
  14. End Sub


of course u have to call it in ur command button code
vb.net Syntax (Toggle Plain Text)
  1. Private sub cmdcalculate_click(.)
  2. call validation
  3. endsub

am still a beginner thats all i can help u any problems contact another members

Remember with other members u'll have to try something first
without effort they will not help u
hope this help
Last edited by manutd4life; Sep 1st, 2008 at 4:44 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster
manutd4life is offline Offline
123 posts
since Dec 2007
May 20th, 2010
0
Re: Factorial Calculate
Note: I am well aware of how old this thread is. However, seeing as this was one of the top links for solving a factorial on VB.NET when googling "VB.NET factorial" i figured i would provide a perhaps better solution than the one offered above.

To calculate a factorial the simplest way and more optimized then the above, you may use the following function

VB.NET Syntax (Toggle Plain Text)
  1. Private Function Factorial(ByVal Num As Int64) As Int64
  2. Factorial = 1
  3. For i = 2 To Num
  4. Factorial *= Num
  5. Next
  6. End Function

Please be aware that there is an inherent max to all factorial calculations. Based on x86 or x64. Let alone the data types being supported. I used a Int64 instead of Integer because Integer is actually a wrapper for Int32, which has a smaller min/max

In some cases, you may want to use:

VB.NET Syntax (Toggle Plain Text)
  1. Private Function Factorial(ByVal Num As UInt64) As UInt64
  2. Dim i As UInt64
  3. Factorial = 1
  4. For i = 2 To Num
  5. Factorial *= Num
  6. Next
  7. End Function

seeing as the UInt64 allows for a MUCH bigger maximum than the Int64 does. U stands for unsigned, meaning its positive numbers only.
Last edited by Kalbintion; May 20th, 2010 at 9:00 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kalbintion is offline Offline
1 posts
since May 2010
Aug 17th, 2011
0

factoril

VB.NET Syntax (Toggle Plain Text)
  1. Private Function Factorial2(ByVal Num As Int64) As Int64
  2.  
  3. If Num <= 1 Then
  4. Return 1
  5. Else
  6. Return Num * Factorial2(Num - 1)
  7. End If
  8.  
  9. End Function
Last edited by adatapost; Aug 17th, 2011 at 11:08 am. Reason: Added [code] tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khaled_eltaweel is offline Offline
1 posts
since Aug 2011
Aug 17th, 2011
0
Re: Factorial Calculate
VB.NET Syntax (Toggle Plain Text)
  1. Public Class frmFactorialCalculator
  2.  
  3. Private Sub btnCalFactorial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalFactorial.Click
  4. Dim intNumber As Integer = 0
  5. Dim IntFactorial As Integer = 0
  6. Try
  7. intNumber = Val(txtNumber.Text.Trim)
  8. IntFactorial = fnGetFactorial(intNumber)
  9. lblresult.Text = IntFactorial
  10. Catch ex As Exception
  11. MessageBox.Show(ex.Message)
  12. End Try
  13. End Sub
  14.  
  15. 'Factorial Function Codes
  16. Private Function fnGetFactorial(ByVal num As Integer) As Integer
  17. Dim intfac As Integer = 1
  18. Dim i As Integer
  19.  
  20. For i = 2 To num
  21. intfac *= i
  22. Next
  23. fnGetFactorial = intfac
  24. End Function
  25.  
  26. End Class
Reputation Points: 43
Solved Threads: 67
Veteran Poster
Netcode is offline Offline
1,016 posts
since Jun 2009
Dec 11th, 2011
0

Factorial

Hello .. im a business student with a vb.net course .. donno why we're being taught vb but that's that .. any ways could anyone help me solve this problem:
Develop an application which reads two integers n1 and n2 from the user via two textboxes and displays in a label the sum of the factorial of the integers n1  n2
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lili22 is offline Offline
2 posts
since Dec 2011
Dec 11th, 2011
0
Re: Factorial Calculate
Well, you've got your factorial code already supplied in this answer. Thats the hard part done, now all you have to do is build the interface. What part are you having trouble with?
Reputation Points: 84
Solved Threads: 120
Practically a Master Poster
hericles is offline Offline
649 posts
since Nov 2007
Dec 12th, 2011
0
Re: Factorial Calculate
Quote ...
Develop an application which reads two integers n1 and n2 from the user via two text-boxes and displays in a label the sum of the factorial of the integers n1  n2
You already have an answer from your own question. You made mention of having two text-boxes to take values, so from there you should know you need two text-boxes on your interface. To add to it, you would need a button which you would write the factorial codes behind it so when clicked, it does the factorial.
Reputation Points: 43
Solved Threads: 67
Veteran Poster
Netcode is offline Offline
1,016 posts
since Jun 2009
Message:
Previous Thread in VB.NET Forum Timeline: VB.Net AutoComplete NOT based on first character
Next Thread in VB.NET Forum Timeline: how to insert database value to a Lable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC