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
xxxxxxxxxx
n = 1
suma = 0
while n <= 10:
x = int (input ("Enter the last %d number: "% n))
suma = suma + x
n = n + 1
print ("suma: %d"% suma)
Activity: 4.1 ActiveCode (ac_l15_4a_en)
Average of 10 integer numbers
xxxxxxxxxx
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))
Activity: 4.2 ActiveCode (ac_l15_4b_en)
Calculate the factorial of ten
Print output (drag lower right corner to resize)
|
Activity: CodeLens 4.3 (cl_l15_4_en)
Calculate the factorial of an integer number
n
xxxxxxxxxx
i = 1
fact = 1
n = int (input ("Enter n: "))
while i <= n:
fact = fact * i
i = i + 1
print ("Fact(%d) =% d"% (n, fact))
Activity: 4.4 ActiveCode (ac_l15_4c_en)
You have attempted 1 of 5 activities on this page