# Python Program to Find Sum of N Natural Numbers

## Getting Started

The task is to find the sum of N natural numbers using python program. For example,

If N = 10, then output should be 55 because sum of first N natural numbers in 55 i.e. 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55.

If N = 5, then output should be 15 because 1 + 2 + 3 + 4 + 5 = 15.

We can do it in below ways –

## Using for loop

We can simply run a python for loop to find sum of N natural numbers as shown below –

```def sum_of_n_numbers(n):
sum = 0
for i in range(1, n + 1):
sum += i
return sum

# Get user input for N

n = int(input("Enter a positive integer N: "))
result = sum_of_n_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
```

Here,

• Initialise a variable – sum by 0.
• Run a for loop from 1 to n.
• In each iteration, add iteration value in sum. For example, in ith iteration, add i in sum.
• Using this way, we will finally get sum of all values from 0 to n.
• Finally, result is printed using print() function.

Output:

```Enter a positive integer N:
12
The sum of the first 12 natural numbers is: 78
```

## Using Arithmetic Formula

There is an arithmetic formula

Sum of N Natural numbers = n * (n + 1) / 2

We can use this arithmetic formula to write python program to find sum of n natural numbers. This best way to find sum of n natural numbers in python program.

```def sum_of_n_numbers(n):
return int(n * (n + 1) / 2)

# Get user input for N
n = int(input("Enter a positive integer N: "))
result = sum_of_n_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
```

Here,

• sum_of_n_numbers method returns sum of N natural numbers.
• Finally, we print the result using print() function.

Output:

```Enter a positive integer N:
5
The sum of the first 5 natural numbers is: 15
```

## Using Recursion

We can write program to find sum of N natural numbers recursively too. It’s just a recursive approach of for loop program.

```def sum_of_n_numbers(n):
if n == 1:
return 1
else:
return n + sum_of_n_numbers(n - 1)

# Get user input for N
n = int(input("Enter a positive integer N: "))
result = sum_of_n_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
```

Here,

• sum_of_n_numbers method is getting called recursively until the last value 1.
• In the last call, this method will return 1.
• After that, second last call will be executed. In this call, 1 + 2 will be returned.
• Subsequently, all method calls will be executed and we will get sum of n numbers.

Output:

```Enter a positive integer N:
4
The sum of the first 4 natural numbers is: 10
```

## Using sum() and list Comprehension

Built-in method sum() returns sum of numbers provided as arguments. We can use sum() method too to write python program to find sum of N natural numbers.

```def sum_of_n_numbers(n):
return sum([i for i in range(1, n + 1)])

# Get user input for N
n = int(input("Enter a positive integer N: "))
result = sum_of_n_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
```

Here,

• sum() method takes list of values as arguments.
• range() method returns immutable sequence numbers between specified start and stop value. Here, it’s between 1 and (n + 1)

Output:

```Enter a positive integer N:
10
The sum of the first 10 natural numbers is: 55
```

## Using while Loop

Instead of python for loop, we can also use python while loop to find sum of N natural numbers as shown below –

```def sum_of_n_numbers(n):
sum = 0
i = 1
while i <= n:
sum += i
i += 1
return sum

# Get user input for N
n = int(input("Enter a positive integer N: "))
result = sum_of_n_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
```

Here,

• sum_of_n_numbers method returns sum of N natural numbers using while loop.
• At first, initialise sum = 0, i = 1.
• Now, run a while loop from i = 1 to i = n.
• In each iteration, add value of i to sum. Then, increment the value i by 1.
• Finally, we get value of variable sum as sum of N natural numbers.
• Returned value is then printed using print() method.

Output:

```Enter a positive integer N:
10
The sum of the first 10 natural numbers is: 55
```

## Using reduce() Function

reduce() function applies a function to the elements of iterable. We can use this trick to write python program to find sum of N natural numbers as shown below –

```from functools import reduce

def sum_of_n_numbers(n):
return reduce(lambda x, y: x + y, range(1, n + 1))

# Get user input for N
n = int(input("Enter a positive integer N: "))
result = sum_of_n_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
```

Output:

```Enter a positive integer N:
12
The sum of the first 12 natural numbers is: 78
```

## Using math.comb() Function

math.comb() is basically a built-in method in math module in python. math.comb(n, k) returns number of ways to choose k items from n without repetition and without order.

Mathematically, math.comb(n, k) is equivalent to n! / (k! * (n – k)!).

So, math.comb(n + 1, 2) is equivalent to

=> (n + 1)! / (2! * ((n + 1) – 2)!)
=> (n + 1)! / (2! * ((n – 1)!)
=> (n + 1) * n * (n – 1)! / (2 * 1 * (n – 1)!)
=> (n + 1) * n * (n – 1)! / (2 * (n – 1)!)
=> (n + 1) * n / 2

Notice that it’s same formula which we have used earlier in this article.

```import math

def sum_of_n_numbers(n):
return math.comb(n + 1, 2)

# Get user input for N
n = int(input("Enter a positive integer N: "))
result = sum_of_n_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
```

Output:

```Enter a positive integer N:
10
The sum of the first 10 natural numbers is: 55
```

Learn about python at official Doc