Jump to content

multiplication problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
kenneth_888

kenneth_888

    Newbie

  • Members
  • Pip
  • 6 posts
hi
struggling to do this simple program:
wanting to find out the sum of the multiplication,
Example: input 5 in textbox it should compute like this 1 * 2 * 3 * 4 * 5 = 120


Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim isconverted As Boolean = False

        Dim b As Integer

        Dim c As Decimal

        isconverted = Integer.TryParse(TextBox1.Text, b)

        c = summation(b)

        Label1.Text = c

    End Sub

    Public Function summation(ByVal N As Integer) As Decimal

      [B]nOT SURE WHAT TO PUT HERE??[/B]

    End Function


THANK YOU

#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
You could do a for/while loop with a counter "i" (initialized to your N = 5 in your example) where you keep multiplying a temporary variable "n" (also initialized to N) with i-1 until i-1 is 0. I'm a bit tired right now, so if I'm missing an obvious solution, please excuse me.. *yawns* Anyway, it's a suggestion.

Posted via CodeCall Mobile

#3
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
It just occured to me that it probably would make more sense to initialize i to N-1 and multiply n with i until i is less than or equal to 1. Sorry for that.

The idea:

Declare var n
n = N
Declare var i
For i = (N - 1) to i <= 1
n = n * i

That should work.

Posted via CodeCall Mobile

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
What you are talking about is a factorial not a summation.

As said above, the easiest way is to run a loop and multiply a variable n by i until each time through the loop. You could also use recursion but that is a different beast.

public int fact(int n) {
    int nResult = 1;
    for (int i=1;i<n;i++) {
         nResult = nResult * i;
    }
    return nResult;
}


#5
kenneth_888

kenneth_888

    Newbie

  • Members
  • Pip
  • 6 posts
i figuare it out


Public Function Summation(ByVal num As Integer) As Decimal

        Dim i As Integer = num

        Dim math As Integer = 1

        Do Until i = 0

            math = math * i

            i = i - 1

        Loop


what u guys think

#6
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Posted via CodeCall Mobile

Basically just what we said, but using a do-until, so: ok. :)