Summing Natural Numbers from 1 to n

  • Share this:

Code introduction


Calculate the sum of natural numbers from 1 to n


Technology Stack : None

Code Type : Function

Code Difficulty :


                
                    
def sum_naturals(n):
    total, current = 0, 1
    while current <= n:
        total += current
        current += 1
    return total                
              
Tags: