4. Accumulators¶
The difference between a counter and an accumulator is that for counters, the added value is constant, and for accumulators, it is variable.
Calculation of the sum of ten integer numbers
suma = 0 while n <= 10: x = int (input (“Enter the last %d number: “% n)) suma = suma + x n = n + 1
print (“suma: %d”% suma)
Average of 10 integer numbers
:nocodelens: :stdin:
n = 0 suma = 0 while n <10: x = int (input (“Enter the last %d number: “% (n + 1))) suma = suma + x n = n + 1
print (“Average: %5.2f”% (suma / n))
Calculate the factorial of ten
i = 1 fact = 1 while i <= 10: fact = fact * i i = i + 1 print (“Fact(10) =% d”% fact)
Calculate the factorial of an integer number
n
:nocodelens: :stdin:
i = 1 fact = 1 n = int (input (“Enter n: “)) while i <= n: fact = fact * i i = i + 1
print (“Fact(%d) =% d”% (n, fact))