I want to calculate factorial with all proper validations. Can anyone tell me the functions to be use for this using VB.NET...pLz
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
Public Sub validation() If txtFirstNum.Text = "" Then MsgBox("First Number is Empty") ElseIf Not IsNumeric(txtFirstNum.Text) Then MsgBox("First Number is not numeric") Else call factorial endif endsub
Public Sub factorial() Dim counter As Integer Dim result As Long Dim x As String = (Val(txtFirstNum.Text)) counter = 1 result = 1 While counter <= x result = result * counter counter = counter + 1 txtAnswer.Text = result End While End Sub
of course u have to call it in ur command button code
Private sub cmdcalculate_click(.) call validation 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
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
Private Function Factorial(ByVal Num As Int64) As Int64 Factorial = 1 For i = 2 To Num Factorial *= Num Next 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:
Private Function Factorial(ByVal Num As UInt64) As UInt64 Dim i As UInt64 Factorial = 1 For i = 2 To Num Factorial *= Num Next 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.
Edited by Kalbintion: n/a
Private Function Factorial2(ByVal Num As Int64) As Int64 If Num <= 1 Then Return 1 Else Return Num * Factorial2(Num - 1) End If End Function
Edited by __avd: Added [code] tags.
Public Class frmFactorialCalculator Private Sub btnCalFactorial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalFactorial.Click Dim intNumber As Integer = 0 Dim IntFactorial As Integer = 0 Try intNumber = Val(txtNumber.Text.Trim) IntFactorial = fnGetFactorial(intNumber) lblresult.Text = IntFactorial Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub 'Factorial Function Codes Private Function fnGetFactorial(ByVal num As Integer) As Integer Dim intfac As Integer = 1 Dim i As Integer For i = 2 To num intfac *= i Next fnGetFactorial = intfac End Function End Class
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
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?
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.