Results 1 to 11 of 11

Thread: Help With Visual Basic

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    3

    Help With Visual Basic

    I need help creating a VB program using factorials

    Here are the instructions:

    Factorials are used to calculate combinations of things in statistics. The factorial of an integer is the product of that integer times all positive numbers less than itself. For example 5 factorial is:

    5 * 4 * 3 * 2 * 1 or 120.

    The symbol for factorials is the exclamation point, so 5 factorial is written like this:

    5!

    By the way, we programmers call the exclamation point a bang. So we would say “5 bang” to describe this. Just another reason to become a programmer. Cool jargon.

    In statistics, factorials are used to calculate permutations and combinations. For example, the odds of getting a particular poker hand can be calculated using factorials.

    As a gambling craze has hit downtown Rome and the Romans are as innumerate as ever, you have decided to help them calculate factorials. Your program will look simple. The Roman will enter an integer, and you will display the factorial of that integer. One problem you need to consider is that factorials get very large very quickly. It doesn’t take a very large integer to have a very large factorial. You need to figure out how large an integer can be held in a VB integer variable and check that you don’t enter a number that’s too large. You will also need to know how to construct a loop. Three loop constructs exist that you can use (you only need one of these):

    Do While

    Do Until

    For – Next

    Before you start your program you consult with a mathematician who informs you that the factorial of zero is 1, and that there is no such thing as factoring a negative number. You thank her and decide to put these checks in your program.

    You will also need to use the integer data type instead of the decimal data type. Variables of type integer can only hold whole numbers – no fractions. They are declared like this:
    Dim intFactorial as int

    Also the integer.tryparse function will probably be useful. Integer.tryparse does what you would expect it to do.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,113

    Re: Help With Visual Basic

    Not sure what to help with.
    Can't see what you've done.
    Don't see any questions.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,953

    Re: Help With Visual Basic

    Also what version of VB are you using? Currently your thread is in the Office Development section, but it doesn't sound like your issue has much to do with Office, but I don't know if it should be in VB6, or .NET or somewhere else still.

    Once we know what version of VB you're working with we can get it moved appropriately.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,953

    Re: Help With Visual Basic

    I just saw this as I posted... since Interg.TryParse is only in VB.NET... we now know where to move the thread...

    Quote Originally Posted by Ad123 View Post
    Also the integer.tryparse function will probably be useful. Integer.tryparse does what you would expect it to do.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,113

    Re: Help With Visual Basic

    I read it, but completely missed it as I already had VBA based limitations and responses swimming around my brain distracting me as I read.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    3

    Re: Help With Visual Basic

    Im using VB Studio 2013. I have no experience with integer.tryparse, only decimal.tryparse and not sure what the difference is. Also, I have no idea how I would make the program create factorials and make the 0 a 1 or how to reject negative numbers
    I would start it like this but not sure if this would work:

    Dim A as Integer
    A = Textbox.Text (will probably use a different name for the text box)
    Textbox.Text = ""
    Dim Success As Boolean
    Success = Integer.tryparse(Textbox.Text,A)

    So now if that were to work i would need help creating the loop and the if-then statements

  7. #7
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,612

    Re: Help With Visual Basic

    Moved to .Net section.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,113

    Re: Help With Visual Basic

    Quote Originally Posted by Ad123 View Post
    Im using VB Studio 2013. I have no experience with integer.tryparse, only decimal.tryparse and not sure what the difference is. Also, I have no idea how I would make the program create factorials and make the 0 a 1 or how to reject negative numbers
    I would start it like this but not sure if this would work:

    Dim A as Integer
    A = Textbox.Text (will probably use a different name for the text box)
    Textbox.Text = ""
    Dim Success As Boolean
    Success = Integer.tryparse(Textbox.Text,A)

    So now if that were to work i would need help creating the loop and the if-then statements
    So, you've never written an If .... Then statement yet?
    Does this "class" have any instructional materials at all?
    If you type in "If" and hit the F1 key in the Visual Studio editor, does the IDE provide any help at all?
    If you do an internet search on VB.Net If statement does that give you any tutorials or examples you can learn from.

    Have you tried just writing down the steps you would take logically, before trying to code it.

    Code:
    If A is less than 0 then 
      Inform the user the input is less than 0 so invalid.
    Else If A is larger than the MaximumInput then
      Inform the user the input is too large to factor
    Else if A = 0 then
      Display 1 as the result
    Else 
      Factor = 1
      Do a loop from 2 to A
         Multiply Factor by loop value
      End of Loop
      Display Factor as the result
    End Ifs
    You can search on VB.Net loops to see what options you have there. A "For loop" should work fine in this case.
    Knowing what the maximum input value is going to be will be the challenging part for most.
    You will probably want to use a type that has a larger range than Integer, and start a loop factoring the value and comparing the result to Integer.MaxValue and as soon as the result exceeds Integer.MaxValue the loop value - 1 is your maximum input value allowed.
    A decimal type has a much larger range than Integer so could be used to find that maximum input value.
    The aswer is 1
    Else if A is larger than MaximumInput then

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    3

    Re: Help With Visual Basic

    Alright so this is what I have so far:

    Dim A As Integer
    Dim Success1 As Boolean
    A = (TxtNmbrBox.Text = "")
    Success1 = Integer.TryParse(TxtNmbrBox.Text, A)

    If Not Success1 Then
    MsgBox("Please Enter A Number In Box")
    TxtNmbrBox.Text = ""
    LblAnswer.Text = ""
    Exit Sub
    End If

    If A = 0 Then
    LblAnswer.Text = "1"
    ElseIf A < 0 Then
    MsgBox("Negative Numbers Can Not Be Factored" + vbCrLf + "Please Enter a Positive Number")

    ElseIf A >= 1 Then
    End If


    I am having trouble making the loop for the factorials and am not sure how to do that.
    Some help creating the loop would be great.
    The code for the loop the teacher wants us to use is
    Do While
    or
    Do Until
    or
    For – Next

  10. #10
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Help With Visual Basic

    First, there is a line that doesn't make sense:
    Code:
    A = (TxtNmbrBox.Text = "")
    A was declared as an Integer, but that line doesn't return an Integer; if it does return anything, it'd return a Boolean letting you know that it successfully set TxtNmbrBox.Text to "". Just delete that line; the next line is the correct way for setting A from whatever was typed in the TextBox using the TryParse as the assignment suggested.

    While looking at the top part of the code, you may also want to declare another Integer for the answer. Although you can make that declaration anywhere before you actually use it, in some ways it's easier to debug if you make all of the declarations at the beginning.

    As for calculating the factorial using a loop, take a look at passel's pseudo-code as that essentially has what you need... Also read up on using different loops (here is a tutorial site that talks about the different loops in VB). Passel mentions that A "For loop" should work fine in this case. so make sure you pay special attention to using those loops, clicking on the For...Next loop in the chart on that page to read the tutorial specific for them.

    One last thing, as mentioned in the assignment:
    Quote Originally Posted by Assignment
    It doesn’t take a very large integer to have a very large factorial. You need to figure out how large an integer can be held in a VB integer variable and check that you don’t enter a number that’s too large.
    So make sure once you get the loop working, change the DataType for your answer's variable to something larger than an Integer (like a Long integer) and determine the maximum input that the user is allowed to use, and verify in your code that the input variable A isn't larger than that maximum limit.

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: Help With Visual Basic

    Here's a quick example I threw together. It is a console application:
    Code:
    Imports System
    Public Module Module1
    	Public Sub Main()
    		Do
    			Console.Write("Value: ")
    			
    			Dim value As Integer			
    			If Integer.TryParse(Console.ReadLine, value) Then
    				Console.WriteLine("The factorial of " & value.ToString() & " is: " & Factorial(value).ToString())
    			Else
    				Console.WriteLine("Please enter a valid number.")
    			End If
    		Loop
    	End Sub
    
    	Private Function Factorial(ByVal value As Integer) As Integer
    		If value < 0 Then
    			Return -1
    		ElseIf value = 0 Then
    			Return 1
    		Else
    			Dim total As Integer = value
    			
    			For x As Integer = value - 1 To 1 Step -1
    				total *= x
    			Next
    
    			Return total
    		End If
    	End Function
    
    End Module

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width