Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc
<span class="token keyword">var</span> number <span class="token operator">=</span> <span class="token number">4</span>
<span class="token keyword">var</span> sumOfNaturalNumber <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token number">1.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>number<span class="token punctuation">{</span>
sumOfNaturalNumber <span class="token operator">=</span> sumOfNaturalNumber <span class="token operator">+</span> j
<span class="token punctuation">}</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Number is - "</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Natural number?s sum is - "</span><span class="token punctuation">,</span>sumOfNaturalNumber<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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 ?
<span class="kwd">for</span><span class="pln"> j </span><span class="kwd">in</span><span class="pln"> </span><span class="lit">1</span><span class="pun">?</span><span class="pln">number</span><span class="pun">{</span><span class="pln">
sumOfNaturalNumber </span><span class="pun">=</span><span class="pln"> sumOfNaturalNumber </span><span class="pun">+</span><span class="pln"> j
</span><span class="pun">}</span><span class="pln">
</span><span class="typ">It</span><span class="pln"> can also be written </span><span class="kwd">as</span><span class="pln">
</span><span class="kwd">for</span><span class="pln"> j </span><span class="kwd">in</span><span class="pln"> </span><span class="lit">1</span><span class="pun">?</span><span class="pln">number</span><span class="pun">{</span><span class="pln">
sumOfNaturalNumber </span><span class="pun">+=</span><span class="pln"> j
</span><span class="pun">}</span>
The working of this loop is ?
sumOfNaturalNumber = 0<br>j = 1<br>sumOfNaturalNumber = 0 + 1 = 1<br>sumOfNaturalNumber = 1<br>j = 2<br>sumOfNaturalNumber = 1 + 2 = 3<br>sumOfNaturalNumber = 3<br>j = 3<br>sumOfNaturalNumber = 3 + 3 = 6<br>sumOfNaturalNumber = 6<br>j = 4<br>sumOfNaturalNumber = 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.
<div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the lower limit -"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> lowerLimit <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the upper limit -"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> upperLimit <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>
<span class="token keyword">var</span> sumOfNaturalNumber <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">for</span> j <span class="token keyword">in</span> lowerLimit<span class="token operator">...</span>upperLimit<span class="token punctuation">{</span>
sumOfNaturalNumber <span class="token operator">=</span> sumOfNaturalNumber <span class="token operator">+</span> j
<span class="token punctuation">}</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered lower limit is - "</span><span class="token punctuation">,</span> lowerLimit<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered upper limit is -"</span><span class="token punctuation">,</span> upperLimit<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Natural number?s sum is - "</span><span class="token punctuation">,</span> sumOfNaturalNumber<span class="token punctuation">)</span>
</div>
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 ?
<span class="kwd">for</span><span class="pln"> j </span><span class="kwd">in</span><span class="pln"> lowerLimit</span><span class="pun">...</span><span class="pln">upperLimit</span><span class="pun">{</span><span class="pln">
sumOfNaturalNumber </span><span class="pun">+=</span><span class="pln"> j
</span><span class="pun">}</span><span class="pln">
</span><span class="typ">It</span><span class="pln"> can also be written </span><span class="kwd">as</span><span class="pln">
</span><span class="kwd">for</span><span class="pln"> j </span><span class="kwd">in</span><span class="pln"> lowerLimit</span><span class="pun">...</span><span class="pln">upperLimit</span><span class="pun">{</span><span class="pln">
sumOfNaturalNumber </span><span class="pun">=</span><span class="pln"> sumOfNaturalNumber </span><span class="pun">+</span><span class="pln"> j
</span><span class="pun">}</span>
The working of this loop is ?
lowerLimit = 5<br>upperLimit = 11<br>sumOfNaturalNumber = 0<br>j = 5<br>sumOfNaturalNumber = 0 + 5 = 5<br>sumOfNaturalNumber = 5<br>j = 6<br>sumOfNaturalNumber = 5 + 6 = 11<br>sumOfNaturalNumber = 11<br>j = 7<br>sumOfNaturalNumber = 11 + 7 = 18<br>sumOfNaturalNumber = 18<br>j = 8<br>sumOfNaturalNumber = 18 + 8 = 26<br>sumOfNaturalNumber = 26<br>j = 9<br>sumOfNaturalNumber = 26 + 9 = 35<br>sumOfNaturalNumber = 35<br>j = 10<br>sumOfNaturalNumber = 35 + 10 = 45<br>sumOfNaturalNumber = 45<br>j = 11<br>sumOfNaturalNumber = 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
<div class="execute"></div><div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation <span class="token keyword">import</span> Glibc <span class="token keyword">var</span> number <span class="token operator">=</span> <span class="token number">25</span> <span class="token keyword">var</span> sumOfNaturalNumber <span class="token operator">=</span> number <span class="token operator">*</span> <span class="token punctuation">(</span>number <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Number is - "</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Sum of the natural numbers is - "</span><span class="token punctuation">,</span> sumOfNaturalNumber<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
