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 first N even numbers
This tutorial will discuss how to write a Swift program to calculate the sum of first N even numbers.
A number that is multiple of 2 or we can say that a number that is completely divisible by 2(that means leaves no remainder) is known as even number. For example, 2, 4, 6, 8, 10, ?etc. are even numbers. We can calculate the sum of the first N even number using the following formula.
Formula
Following is the formula for the sum of first N even numbers ?
Sum = N(N + 1)
Below is a demonstration of the same ?
Input
Suppose our given input is ?
Number - 6
Output
The desired output would be
2+4+6+8+10+12 = 42 or 6*(6 + 1) = 42 Sum of first 6 even numbers is 42
Algorithm
Following is the algorithm ?
Step 1 ? Create a function.
Step 2 ? Declare a variable named "sum" to store the sum of the first N even numbers using the mathematical formula ?
let sum = a * (a + 1)
Step 3 ? Return the sum
Step 4 ? Declare a variable named "num". Here the value of "num" can be use r defined or pre defined.
Step 5 ? Call the function and pass "num" as an argument to the function.
Step 6 ? Print the output.
Example 1
The following program shows how to calculate the sum of first N even numbers.
<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 comment">// Function to calculate the sum of first N even numbers </span>
func <span class="token function">sumEvenNum</span><span class="token punctuation">(</span>a <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Int<span class="token punctuation">{</span>
<span class="token comment">// Finding the sum of first N even numbers </span>
<span class="token keyword">let</span> sum <span class="token operator">=</span> a <span class="token operator">*</span> <span class="token punctuation">(</span>a <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> sum
<span class="token punctuation">}</span>
<span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">16</span>
<span class="token comment">// Calling the function and displaying the output </span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"\nSum of first \(num) even numbers: "</span><span class="token punctuation">,</span> <span class="token function">sumEvenNum</span><span class="token punctuation">(</span>a<span class="token operator">:</span>num<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Sum of first 16 even numbers: 272
In the above code, we create a function named sumEvenNum(). It takes one argument and returns the sum of first N even numbers using the following mathematical formula ?
sum = a * (a + 1)
So the working of the above code is:
sumEvenNum(16): sum = 16 * (16 + 1) = 16 * 17 = 272
Or we can say the sum of the first 16 even numbers is
0+2+4+6+8+10+12+14+16+18+20+22+24+26+28+30 = 272
Example 2
The following program shows how to calculate the sum of the first N even numbers.
<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 comment">// Function to calculate the sum of first N even numbers</span>
func <span class="token function">sumEvenNum</span><span class="token punctuation">(</span>a <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Int<span class="token punctuation">{</span>
<span class="token comment">// Finding the sum of first N even numbers </span>
<span class="token keyword">let</span> sum <span class="token operator">=</span> a <span class="token operator">*</span> <span class="token punctuation">(</span>a <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> sum
<span class="token punctuation">}</span>
<span class="token comment">// Taking input from the user </span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the number:"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> num <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 comment">// Calling the function and </span>
<span class="token comment">// Displaying output </span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"\nSum of first \(num) even numbers: "</span><span class="token punctuation">,</span> <span class="token function">sumEvenNum</span><span class="token punctuation">(</span>a<span class="token operator">:</span>num<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div>
STDIN Input
Please enter the number: 10
Output
Sum of first 10 even numbers: 110
In the above code, we create a function named sumEvenNum(). It takes one argument. This function is used to find the sum of first N even numbers using the following mathematical formula ?
sum = a * (a + 1)
Here we take the input from the user using readLine() function and pass that input in the sumEvenNum() function.
So the working of the above code is ?
Input = 10 sumEvenNum(10): sum = 10 * (10 + 1) = 10 * 11 = 110
Or we can say the sum of the first 10 even numbers is
0+2+4+6+8+10+12+14+16+18 = 110Or we can say the sum of the first 10 even numbers isĀ 0+2+4+6+8+10+12+14+16+18 = 110
