0

I am trying to create an algorithm in Python that takes an input and prints the multiple of all the numbers less than it. example: input ; 3 output ; 1*2*3 = 6.

example2: input ; 15 output ; 1*2*3*4*5*6*7*8*9*10*11*12*13*14*15 = 1,307,674,368,000.

FatihAkici
  • 4,679
  • 2
  • 31
  • 48
  • 1
    Possible duplicate of [How can I multiply all items in a list together with Python?](https://stackoverflow.com/questions/13840379/how-can-i-multiply-all-items-in-a-list-together-with-python) – AER Oct 29 '19 at 01:00
  • 2
    Welcome to Stack Overflow. Ideally questions would have an attempt from yourself, and have been searched for already. "multiply all numbers in a list python" was an autocomplete in Google. This is why I'm marking as duplicate. Nothing personal, it just makes looking for answers more effective. – AER Oct 29 '19 at 01:01
  • You are trying to calculate factorials. Google it and you'll find plenty of answers. – FatihAkici Oct 29 '19 at 01:22

2 Answers2

0

Welcome to Stack Overflow. I think this problem is not so difficult, and you should try it yourself first.

Here is a simple example:

def main(arg):
    ans = 1
    for x in range(1, arg+1):
        ans *= x
        if x == arg:
            print(x, end=' ') # skip the last one
        else:
            print(x, end='*') 

    print(f'= {ans}', end='')


if __name__ == '__main__':
    try:
        arg = int(input())
        main(arg)
    except:
        pass

input: 3 output: 1*2*3 = 6

taseikyo
  • 126
  • 7
0

This is equal to calculating factorial(n). I will provide a simple recursive implementation.

def recursive_factorial( n ):
    if n <= 1:
        return 1
    return n * recursive_factorial(n - 1)

if __name__ == '__main__':
    x = int(input())
    print( recursive_factorial(x) )

It is a really common programming exercise and you can easily find more information. A good place to start is ( https://en.wikipedia.org/wiki/Factorial )