We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,497 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Factorial Calculate

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

7
Contributors
7
Replies
7 Years
Discussion Span
1 Year Ago
Last Updated
9
Views
deepsmehta
Newbie Poster
1 post since Sep 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

manutd4life
Junior Poster
123 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

Kalbintion
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
khaled_eltaweel
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Netcode
Veteran Poster
1,037 posts since Jun 2009
Reputation Points: 43
Solved Threads: 70
Skill Endorsements: 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

lili22
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

hericles
Veteran Poster
1,065 posts since Nov 2007
Reputation Points: 156
Solved Threads: 228
Skill Endorsements: 9

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.

Netcode
Veteran Poster
1,037 posts since Jun 2009
Reputation Points: 43
Solved Threads: 70
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0732 seconds using 2.66MB