CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Help!

  1. #1
    Join Date
    Mar 2015
    Posts
    2

    Help!

    This is my assignment but I am totally completely lost. If someone can guide me through this, it will be much appreciated!!



    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
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help!

    Looks pretty simple, you just need to construct a loop that will multiply by each number needed and keep track of the total.

    Show what you have so far
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Mar 2015
    Posts
    2

    Re: Help!

    This might be 100% off but I am really confused haha

    Code:
        Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    
            Dim intfactorial As Integer
    
            Dim Success As Boolean
    
            intfactorial = (txtInput.Text = "")
    
            Success = Integer.TryParse(txtInput.Text, intfactorial)
    
            Do While (intfactorial = 0)
                lblAnswer.Text = 1
            Loop
    
            Do While (intfactorial > 0)
                lblAnswer.Text = intfactorial
            Loop
    
            Do While (intfactorial < 0)
                MsgBox("Cannot use negative numbers")
            Loop
    
    
        End Sub

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help!

    Yeah that is pretty far off. Should result in an endless loop.

    Where is the math?

    Your var intfactorial will be set to true if the text box is empty or false if it is not empty
    You code will then arrive at one of the loops and will stay there looping until you end task.

    It will not even try to find the factors as there is no math being done, just display and looping.

    You must do math in the loop and keep track of the total.
    You should use a for next loop for this.
    You only need one loop
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Help!

    Just a note on your Do While loops ( that are not needed here as DM pointed out ). You need to change the values of the object being checked inside a do while loop. A do while loop is known as a conditional loop. Why? Because it loops while a certain condition exists. A For loop on the other hand is a counter loop, where a variables value is incremented automatically, and it will only loop the number of times you tell it to loop.

    BTW, this is the VB 6 forum, the VB.NET forum can be found here : ( for next time )

    http://forums.codeguru.com/forumdisp...sual-Basic-NET

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help!

    Thread moved to .Net forum
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Mar 2015
    Location
    Bangladesh
    Posts
    18

    Re: Help!

    So sad! How did you lost this?

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help!

    Quote Originally Posted by alias2002 View Post
    So sad! How did you lost this?
    What?
    Always use [code][/code] tags when posting code.

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

Featured