1,105,447 Community Members

Factorial Calculate

Member Avatar
deepsmehta
Newbie Poster
1 post since Sep 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want to calculate factorial with all proper validations. Can anyone tell me the functions to be use for this using VB.NET...pLz

Member Avatar
manutd4life
Junior Poster
123 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

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

FACTORIAL

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

Member Avatar
Kalbintion
Newbie Poster
1 post since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
khaled_eltaweel
Newbie Poster
1 post since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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
Member Avatar
Netcode
Veteran Poster
1,049 posts since Jun 2009
Reputation Points: 33 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 8 [?]
 
0
 
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
Member Avatar
lili22
Newbie Poster
2 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
hericles
Veteran Poster
1,078 posts since Nov 2007
Reputation Points: 98 [?]
Q&As Helped to Solve: 232 [?]
Skill Endorsements: 14 [?]
Featured
 
0
 

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?

Member Avatar
Netcode
Veteran Poster
1,049 posts since Jun 2009
Reputation Points: 33 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 8 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article