Multiplying in a loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flyaway888
    New Member
    • Mar 2007
    • 35

    Multiplying in a loop

    Hey.
    My program inputs a number (n) and then outputs the result of all of the numbers up to and including n multiplied together.
    E.g.
    n = 7
    result = 1 * 2 * 3 * 4 * 5 * 6 * 7

    I know that I need to use a loop to get all the numbers but then how do I multiply them all together?
    Thanks
  • chella
    New Member
    • Mar 2007
    • 51

    #2
    Originally posted by flyaway888
    Hey.
    My program inputs a number (n) and then outputs the result of all of the numbers up to and including n multiplied together.
    E.g.
    n = 7
    result = 1 * 2 * 3 * 4 * 5 * 6 * 7

    I know that I need to use a loop to get all the numbers but then how do I multiply them all together?
    Thanks
    Hi,
    The logic to obtain this is simple. Have a for loop with the condition (i<=n)
    Inside the for loop have 'res=res*i' (initialize res to 1)
    The 'res' will have the expected value.

    Regards,
    chella

    Comment

    • flyaway888
      New Member
      • Mar 2007
      • 35

      #3
      Sorry it took awhile but thanks so much for that.

      Comment

      Working...