Results 1 to 8 of 8

Thread: Just tell me, what is my teacher talking about?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    Post

    You are to write a function that will calculate a factorial of a number from 1 to 150. The user should enter a number followed by a ! the symbol for a factorial in a text box on a form. When the user clicks a command button you should error check for a blank box, or other inappropriate entries. Once satisfied that the data is valid, the code should call a function called MYFACTORIAL. Once the factorial has been calculated the result should be placed in a label called lblfactorial.
    To calculate a factorial you multiply each number from 1 up to and including the number which the factorial is being taken of. For example:
    6! Would be calculated thusly: 1 x 2 x 3 x 4 x 5 x 6
    6! = 720
    11! Would be calculated thusly: 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11
    11! = 39,916,800
    Below is an example of a function that takes to numbers and returns a number that has been truncated to the number of decimals that was specified:
    Public Function Truncate_Decimals(numberin As Single, places As Integer)
    'cuts off decimals after a specified number of places
    Truncate_Decimals = Int(numberin * 10 ^ places) / 10 ^ places
    End Function
    If the user enters 3.123456789 in a text box a program could use this function to trim it to any number of decimal places to the right of the decimal point. The result of using this function would be 3.1234 in the variable Trimmednumber.
    Trimmednumber = Truncate_decimals(val(txtnumberin.text),4) ‘ trims number in text box to four digits to theright of the decimal place

  2. #2
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    Ok, so what exactly is your question?

    ------------------
    Thanks,
    Ryan
    [email protected]
    ICQ# 47799046

  3. #3
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305

    Post

    and why should we do your homework? if you have a specific question...

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    Post

    Just someone tell me. How could I possibly make the number 6, be calculated as:

    1*2*3*4*5*6.

    I don't quite understand how I would go about this. Otherwise I understand the project completely.

    ------------------
    "I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"

  5. #5
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    ok, here's what you would do. text1 is where the number is inserted that you want to find the factorial of. text2 is where the answer will be displayed.

    Dim x
    factorial = text1.text
    For i = 1 to factorial
    x = x * i
    Next i
    Text2.text = x

    There you go, it's that simple. Just put this code where you want the actual calculation to be carried out, probably a button that's marked "calculate" or something like that. Have fun on your homework.

    ------------------
    Thanks,
    Ryan
    [email protected]
    ICQ# 47799046

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    Post

    Surely, it's not that simple because x is always equal to zero. Because it's value never increases. So everytime it calculates, here is what the program will see, numberwise.

    Dim x(which at this point is given the default of zero)

    factorial = a number, let's say 3

    For i = 1 to 3

    x(which equals zero) = x(zero) * 1

    'This results in x = 0

    Next

    then it goes again,

    x(which still equals zero) = x(zero) * 2

    'So now, even though the number you were multiplying x by increases, x still equals zero.

    Text2.text = x 'Which at this point, still contains no value.

    I might be wrong, is there anyone who can show me otherwise and make me look like a total fool? Please say yes.

    ------------------
    "I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"

  7. #7
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    Ok, well then simply put this statement after Dim x:

    x=1

    That will solve your problem

    ------------------
    Ryan
    [email protected]
    ICQ# 47799046

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    Post

    Did that, now I have another problem.

    How do I check what the last character in a string is?

    So far I've got:

    dim length as integer
    dim original as string
    dim dog as string

    original = "Bob!"

    length = len(original)

    dog = mid(original, length,1)

    if dog like "!", wait...I've got it. Never mind.



    ------------------
    "I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"

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