sum of n numbers algorithm code example
Example 1: Determine the sum of al digits of n
def sum_of_digits(n):
sum = 0
while (n != 0):
sum = sum + int(n % 10)
n = int(n/10)
return sum
Example 2: formula for sum of n numbers
Sum of the First n Natural Numbers. We prove the formula 1+ 2+ ... + n = n(n+1) / 2, for n a natural number