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 6 Years Ago 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 5 Years Ago 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.
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Here is the sample output that I ...
Hello All ...
Iam Getting An Error With try to excecute the stored procedure .
I have Have Sql database , the stored procedure like so :
USE [MPRS] GO /****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE ...
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...