Swift Program to calculate the sum of first N odd numbers

This tutorial will discuss how to write a Swift program to find the sum of first N odd numbers.

A number that is not divisible by 2 or we can say that when an odd number is divided by 2 then it leaves some remainder such type of number is known as an odd number. For example, when 2 divides by 2 leave remainder 0 whereas 3 divides by 2 leaves remainder 1. So it means 2 is even number and 3 is an odd number.

List of odd numbers is:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, ??

We can calculate the sum of the first N odd numbers using the following mathematical formula.

Formula

Following is the formula for the sum of the first N odd numbers ?

Sum = N * N

Below is a demonstration of the same ?

Input

Suppose our given input is ?

Number - 6

Output

The desired output would be ?

1+3+5+7+9+11 = 36 or 6*6 = 36
Sum of first 6 odd numbers is 36

Algorithm

Following is the algorithm ?

  • Step 1 ? Create a function.

  • Step 2 ? Declare a variable named "sum" to store the sum of first N odd numbers using the mathematical formula ?

let sum = a * a
  • Step 3 ? Return the sum

  • Step 4 ? Declare a variable named "num". Here the value of "num" can be user 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 find the sum of first N odd 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 Odd numbers </span>
func <span class="token function">sumOddNum</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 keyword">let</span> sum <span class="token operator">=</span> a <span class="token operator">*</span> a
   <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">12</span>

<span class="token comment">// Calling function and displaying output </span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Sum of first \(num) odd numbers: "</span><span class="token punctuation">,</span> <span class="token function">sumOddNum</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 12 odd numbers: 144

In the above code, we create a function named sumOddNum(). It takes one argument and returns the sum of first N odd numbers using the following mathematical formula ?

sum = a * a

So the working of the above code is ?

sumEvenNum(12): sum = 12 * 12 = 144

Or we can say the sum of first 12 odd numbers is

1+3+5+7+9+11+13+15+17+19+21+23 = 144

Example 2

The following program shows how to find the sum of first N odd 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 Odd numbers </span>
func <span class="token function">sumOddNum</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 keyword">let</span> sum <span class="token operator">=</span> a <span class="token operator">*</span> a
   <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(N):"</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 function and displaying output </span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"\nSum of first \(num) odd numbers: "</span><span class="token punctuation">,</span> <span class="token function">sumOddNum</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(N): 15

Output

Sum of first 15 odd numbers: 225

In the above code, we create a function named sumOddNum(). It takes one argument and returns the sum of first N odd numbers using the following mathematical formula ?

sum = a * a

Here we take the input from the user using readLine() function and pass that input in the sumOddNum() function.

So the working of the above code is:

Input = 15 
sumEvenNum(15): sum = 15 * 15 = 225

Or we can say the sum of first 10 even number is

1+3+5+7+9+11+13+15+17+19+21+23+25+27+29 =225
Updated on: 2022-08-25T08:51:55+05:30

620 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements