Golang program to calculate the sum of all odd numbers up to N

In this tutorial, we will be discussing the program to find the sum of all odd numbers up to ?N? in Golang programming.

But before writing the code for this, let?s have a brief discussion on odd numbers.

Odd Numbers

An odd number is a number that is not completely divisible by 2 and its remainder is always 1 when the number is divided by 2. A simple way to check whether a number is an odd number or not is to check its last digit. If the last digit is 1, 3, 5, 7, or 9, then the number is said to be an odd number.

Approach 1: Iterate through the first n odd numbers and add them

Example

  • Number = 4

    Sum of first 4 odd numbers = 16

    First 4 odd numbers are- 1, 3, 5, and 7

    Sum of these numbers = 1 + 3 + 5 + 7

    = 16

Algorithm

Step 1 ? Declare a variable ?N? for storing the number.

Step 2 ? Declare a variable ?nextOdd? for storing the number 1.

Step 3 ? Declare a variable ?sum? for storing the sum of odd numbers and initialize it with 0.

Step 4 ? Create a for loop starting from 1 and run it till it is less than or equal to ?N?.

Step 5 ? Calculate the sum by adding the next odd number back to the sum until the for loop ends.

Step 6 ? All odd numbers have a difference of 2. Therefore, calculate the next odd number by adding 2 to the existing number.

Step 7 ? Once the for loop ends, print the result stored in ?sum?.

Example

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package allows us to print formatted strings</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to calculate the sum of all odd numbers up to N \n"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// declaring variable ?N? for storing the number</span>
   <span class="token keyword">var</span> <span class="token class-name">N</span> <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">10</span>
   
   <span class="token comment">// declaring variable ?nextOdd? for storing the next odd number</span>
   <span class="token keyword">var</span> nextOdd <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">1</span>
   
   <span class="token comment">// declaring variable ?sum? for storing the sum of all odd numbers up to N</span>
   <span class="token keyword">var</span> sum <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">0</span>
   <span class="token keyword">for</span> x <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> x <span class="token operator"><=</span> <span class="token class-name">N</span><span class="token punctuation">;</span> x<span class="token operator">++</span> <span class="token punctuation">{</span>
      
      <span class="token comment">// calculating the I?m sum of odd numbers</span>
      sum <span class="token operator">=</span> sum <span class="token operator">+</span> nextOdd
      
      <span class="token comment">// calculating the next odd number</span>
      nextOdd <span class="token operator">=</span> nextOdd <span class="token operator">+</span> <span class="token number">2</span>
   <span class="token punctuation">}</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Finding the sum of first"</span><span class="token punctuation">,</span> <span class="token class-name">N</span><span class="token punctuation">,</span> <span class="token string">"Odd numbers"</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Therefore, Sum of all odd numbers up to N : "</span><span class="token punctuation">,</span> sum<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

Program to calculate the sum of all odd numbers up to N 

Finding the sum of first 10 Odd numbers
Therefore, Sum of all odd numbers up to N :  100

Description of code

  • var nextOdd int = 1 ? We will be storing the value of the next odd number in this variable. Initially, we assigned the value 1 to it because the odd numbers start from the number 1.

  • var sum int = 0 ? We will be storing the resultant sum of the odd numbers in his variable and will initialize it with 0 in the beginning.

  • for x := 1; x <= N; x++ ? This for loop will be used to count the sum of the odd numbers. It will start from 1 and will run until the value of ?x? is less than or equal to ?N?.

    • sum = sum + nextOdd ? In this, we are calculating the sum by adding the next odd number back to sum until the for loop ends.

    • nextOdd = nextOdd + 2 ? This is used to calculate the next odd number. All odd numbers have a difference of 2, that is why we are adding 2 in order to generate the next odd number.

  • fmt.Println("Therefore, Sum of odd numbers up to N : ", sum) ? printing the calculated sum of all odd numbers until ?N?.

Approach 2: Using the formula for calculating the sum of all odd numbers

Formula

$$\mathrm{Sum\: of\: first\: 'n' \: odd \: numbers\, =\, n \ast n}$$

Example

  • Number = 10

    Sum of first 10 odd numbers = N * N

    = 10 * 10

    = 100

Algorithm

Step 1 ? Declare a variable for storing the number- ?N?.

Step 2 ? Declare a variable for storing the sum of all odd numbers- ?sum? and initialize it with 0.

Step 3 ? Calculate the sum using the formula N * N and store it in the ?sum? variable in the function calculateSumOfOddNumbers().

Step 4 ? Print the calculated sum, i.e, the value stored in the variable ?sum?.

Example

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package allows us to print formatted strings</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">calculateSumOfOddNumbers</span><span class="token punctuation">(</span><span class="token class-name">N</span> <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token keyword">int</span> <span class="token punctuation">{</span>
   
   <span class="token comment">// declaring variable ?sum? for storing the sum of all odd numbers</span>
   <span class="token keyword">var</span> sum <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">0</span>
   
   <span class="token comment">// calculating the sum</span>
   sum <span class="token operator">=</span> <span class="token class-name">N</span> <span class="token operator">*</span> <span class="token class-name">N</span>
   <span class="token keyword">return</span> sum
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   
   <span class="token comment">// declaring variable ?N? for storing the number</span>
   <span class="token keyword">var</span> <span class="token class-name">N</span> <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">10</span>
   <span class="token keyword">var</span> sum <span class="token keyword">int</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to calculate the sum of all odd numbers up to N \n"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// calling function calculateSumOfOddNumbers() for</span>
   <span class="token comment">// calculating the sum of all odd numbers</span>
   sum <span class="token operator">=</span> <span class="token function">calculateSumOfOddNumbers</span><span class="token punctuation">(</span><span class="token class-name">N</span><span class="token punctuation">)</span>
   
   <span class="token comment">// printing the results</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Finding the sum of first"</span><span class="token punctuation">,</span> <span class="token class-name">N</span><span class="token punctuation">,</span><span class="token string">"Odd numbers"</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Therefore, Sum of all odd numbers up to N : "</span><span class="token punctuation">,</span> sum<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

Program to calculate the sum of all odd numbers up to N 

Finding the sum of first 10 Odd numbers
Therefore, Sum of all odd numbers up to N :  100

Description of code

  • calculateSumOfOddNumbers(N int) int ? This is the function where we are calculating the sum of odd numbers. The function has the variable ?N? of data type int as the parameter and also has a return type value of data type int.

  • sum = N * N ? We are finding the sum of odd numbers using this formula- N * N.

  • return sum ? for returning the calculated sum of all odd numbers.

  • sum = calculateSumOfOddNumbers(N) ? We are calling the function calculateSumOfOddNumbers() and storing the calculated value in the ?sum? variable.

  • fmt.Println("Therefore, Sum of odd numbers : ", sum) ? printing the calculated sum of all odd numbers.

Conclusion

This is all about calculating the sum of odd numbers up to N using two approaches. The second approach is better in terms of time complexity and modularity. You can explore more about Golang programming using these tutorials.

Updated on: 2022-11-21T06:26:06+05:30

670 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements