# Swift Program to Calculate the Sum of Natural Numbers

This tutorial will discuss how to write a swift program to calculate the Sum of Natural Numbers.

Natural numbers are the set of whole numbers excluding 0. In other words, natural numbers are the numbers which includes all the positive numbers starting from 1 to infinity. It does not contain negative numbers and 0 in it. For example 2, 3, 67, 49, etc. They are also known as counting numbers. We can find the sum of natural numbers by adding given natural numbers using arithmetic addition operator (+). For example, we have 8 so the sum of natural numbers is 8 + 7 + 6 + 5 + 4 + 3 + 2 +1 = 36

## Algorithm to Calculate the Sum of Natural Numbers

• Step 1 − Define two variables

• Step 2 − Assign the value of those variables

• Step 3 − Create a for loop that will add the number in series

• Step 4 − Print the output

We can find the sum of natural numbers using the following methods −

Sum of Natural Numbers Using for loop

We can calculate the sum of natural numbers using the for loop. In the for loop, we first iterate through every numbers starting from 1 to N and then find the sum of the natural numbers.

Sum of Natural Numbers of user defined range

We can calculate the sum of natural numbers of a user defined range. Here user specifies the upper and lower limit and get the sum of the natural numbers present in between the given range. For example the lower limit is 6 and upper limit is 10 so the sum of natural number is 6 + 7 + 8 + 9 +10 = 40.

Sum of Natural Numbers Using mathematical formula

We can calculate the sum of natural numbers using the mathematical formula.

## Formula

Following is mathematic formula for finding Natural numbers

Sum = Num(Num + 1)/2

Where Num is the natural number

## Sum of Natural Numbers Using for loop

### Example

The following program shows how to calculate Sum of Natural Numbers Using for loop

import Foundation
import Glibc

var number = 4
var sumOfNaturalNumber = 0
for j in 1...number{
sumOfNaturalNumber = sumOfNaturalNumber + j
}

print("Number is - ", number)
print("Natural number’s sum is - ",sumOfNaturalNumber)


### Output

Number is - 4
Natural number’s sum is - 10


Here in the above code, the number is 4 and so a for loop is run from 1 to number that is 1 to 4 and every time the loop iterates j is added to the sumOfNaturalNumber like as shown in the below code −

for j in 1…number{
sumOfNaturalNumber = sumOfNaturalNumber + j
}
It can also be written as
for j in 1…number{
sumOfNaturalNumber += j
}

The working of this loop is −

sumOfNaturalNumber = 0j = 1sumOfNaturalNumber = 0 + 1 = 1sumOfNaturalNumber = 1j = 2sumOfNaturalNumber = 1 + 2 = 3sumOfNaturalNumber = 3j = 3sumOfNaturalNumber = 3 + 3 = 6sumOfNaturalNumber = 6j = 4sumOfNaturalNumber = 6 + 4 = 10

So this is how we get the sum of the natural numbers which is 10.

## Sum of Natural Numbers of user defined range

### Example

The following program shows how to calculate Sum of Natural Numbers of user defined range.

import Foundation
import Glibc

print("Please enter the lower limit -")

print("Please enter the upper limit -")

var sumOfNaturalNumber = 0
for j in lowerLimit...upperLimit{
sumOfNaturalNumber = sumOfNaturalNumber + j
}

print("Entered lower limit is - ", lowerLimit)
print("Entered upper limit is -", upperLimit)
print("Natural number’s sum is - ", sumOfNaturalNumber)


### STDIN Input

Please enter the lower limit -
5
Please enter the upper limit -
11


### Output

Entered lower limit is - 5
Entered upper limit is - 11
Natural number’s sum is - 56


Here in the above code user enter lower limit = 5 and upper limit = 11. Now a for loop is run from lower limit to upper limit that is 5 to 11 and every time the loop iterates j is added to the sumOfNaturalNumber like as shown in the below code snippet −

for j in lowerLimit...upperLimit{
sumOfNaturalNumber += j
}
It can also be written as
for j in lowerLimit...upperLimit{
sumOfNaturalNumber = sumOfNaturalNumber + j
}

The working of this loop is −

lowerLimit = 5upperLimit = 11sumOfNaturalNumber = 0j = 5sumOfNaturalNumber = 0 + 5 = 5sumOfNaturalNumber = 5j = 6sumOfNaturalNumber = 5 + 6 = 11sumOfNaturalNumber = 11j = 7sumOfNaturalNumber = 11 + 7 = 18sumOfNaturalNumber = 18j = 8sumOfNaturalNumber = 18 + 8 = 26sumOfNaturalNumber = 26j = 9sumOfNaturalNumber = 26 + 9 = 35sumOfNaturalNumber = 35j = 10sumOfNaturalNumber = 35 + 10 = 45sumOfNaturalNumber = 45j = 11sumOfNaturalNumber = 45 + 11 = 56

So this is how we get the sum of the natural numbers starting from 5 to 11 which is 56.

## Sum of Natural Numbers Using mathematical formula

### Example

import Foundation
import Glibc

var number = 25
var sumOfNaturalNumber = number * (number + 1)/2

print("Number is - ", number)
print("Sum of the natural numbers is - ", sumOfNaturalNumber)


### Output

Number is - 25
Sum of the natural numbers is - 325


In the above code we calculate the sum of natural number using the mathematical formula −

var sumOfNaturalNumber = number * (number + 1)/2


Here the number is 25 so the sum of natural numbers are 325.