👀 Voorbeeld - Product

Het volgende stukje code berekent het product n · x door herhaaldelijk de optelling te gebruiken: n · x = x + x + ⋯ + x.

print('Geef x')
x = int(input())
print('Geef n')
n = int(input())
product = 0
while n > 0:
  product += x
  n -= 1
print(product)

Voorbeelden:

  • 3 · 5 = 5 + 5 + 5 = 15
  • 5 · 3 = 3 + 3 + 3 + 3 + 3 = 15
  • 9 · 2 = 9 + 9 = 18

💻 Programmeeroefening - Machtsverheffing

Pas deze code aan om de machtsverheffing x tot de n’de macht te programmeren. Ga ervan uit dat n altijd positief is.