- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to calculate the sum of all even numbers
In this tutorial, we will be discussing the program to find the sum of all even numbers in Golang programming.
But before writing the code for this, let’s briefly discuss even numbers.
Even Numbers
An even number is a number that is completely divisible by 2 and its remainder is always 0. A simple way to check whether a number is an even number or not is to check its last digit. If the last digit is 0, 2, 4, 6, or 8, then the number is said to be an even number.
Iterate through the first n even numbers and add them
Example
Number = 4
Sum of first 4 even numbers = 20
First 4 even numbers are- 2, 4, 6, and 8
Sum of these numbers = 2 + 4 + 6 + 8
= 20
Algorithm
Step 1 − Declare a variable ‘num’ for storing the number.
Step 2 − Declare a variable ‘nextEven’ for storing the number 2.
Step 3 − Declare a variable ‘sum’ for storing the sum of even 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 ‘num’.
Step 5 − Calculate the sum by adding the next even number back to the sum until the for loop ends.
Step 6 − All even numbers have a difference of 2. Calculate the next even number by adding 2 to the current number.
Step 7 − Once the for loop ends, print the result stored in ‘sum’.
Example
package main // fmt package allows us to print formatted strings import "fmt" func main() { fmt.Println("Program to calculate the sum of all even numbers \n") // declaring variable ‘num’ for storing the number var num int = 4 // declaring variable ‘nextEven’ for storing the next even number var nextEven int = 2 // declaring variable ‘sum’ for storing the sum of all even numbers var sum int = 0 for x := 1; x <= num; x++ { // calculating sum of even numbers sum = sum + nextEven // calculating next even number nextEven = nextEven + 2 } // printing the results fmt.Println("Finding the sum of first", num, "Even numbers") fmt.Println("Therefore, Sum of even numbers : ", sum) }
Output
Program to calculate the sum of all even numbers Finding the sum of first 4 Even numbers Therefore, Sum of even numbers : 20
Description of code
var num int = 4 − In this line, we are declaring the variable num for storing the number for which the user wants to calculate the sum of all even numbers.
var nextEven int = 2 − We will store the value of the next even number in this variable. Initially, we assigned the value 2 to it.
var sum int = 0 − We will be storing the resultant sum of the even numbers in this variable and will initialize it with 0 in the beginning.
for x := 1; x <= num; x++ − This for loop will be used to count the sum of the even numbers. It will start from 1 and will run until the value of ‘x’ is less than or equal to ‘num’.
sum = sum + nextEven − In this, we are calculating the sum by adding the next even number back to sum until the for loop ends.
nextEven = nextEven + 2 − This is used to calculate the next even number. All even numbers have a difference of 2, that is why we are adding 2 in order to generate the next even number.
fmt.Println("Therefore, Sum of even numbers : ", sum) − printing the calculated sum of all even numbers until ‘num’.
Using the formula for calculating the sum of all even numbers
Formula
$$\mathrm{Sum\: of\: \: first\: 'n'\: even\: numbers = n \ast (n + 1)}$$
Example
Number = 10
Sum of first 10 even numbers = num * (num + 1)
10 * (10 + 1)
= 110
Algorithm
Step 1 − Declare a variable for storing the number- ‘num’.
Step 2 − Declare a variable for storing the sum of all even numbers- ‘sum’ and initialize it with 0.
Step 3 − Calculate the sum using the formula num * (num + 1) and store it in the ‘sum’ variable in the function calculateSumOfEvenNumbers().
Step 4 − Print the calculated sum, i.e, the value stored in the variable ‘sum’ by calling the calculateSumOfEvenNumbers() function from within the main() function.
Example
package main // fmt package allows us to print formatted strings import "fmt" func calculateSumOfEvenNumbers(num int) int { // declaring variable ‘sum’ for storing the sum of all even numbers var sum int = 0 // calculating sum sum = num * (num + 1) return sum } func main() { // declaring variable ‘num’ for storing the number var num int = 10 var sum int fmt.Println("Program to calculate the sum of all even numbers \n") // calling function calculateSumOfEvenNumbers() for // calculating the sum of even numbers sum = calculateSumOfEvenNumbers(num) // printing the results fmt.Println("Finding the sum of first", num, "Even numbers") fmt.Println("Therefore, Sum of even numbers : ", sum) }
Output
Program to calculate the sum of all even numbers Finding the sum of first 10 Even numbers Therefore, Sum of even numbers : 110
Description of code
var num int = 10, var sum int − In this line, we are declaring the variables ‘num’ and ‘sum’.
calculateSumOfEvenNumbers(num int) int − This is the function where we are calculating the sum of even numbers. The function has the variable ‘num’ of data type int as the parameter and also has a return type value of data type int.
sum = num * (num + 1) − We are finding the sum of all even numbers using this formula- num * (num + 1).
return sum − for returning the calculated sum of all even numbers
sum = calculateSumOfEvenNumbers(num) − We are calling the function calculateSumOfEvenNumbers() and storing the calculated value in the ‘sum’ variable.
fmt.Println("Therefore, Sum of even numbers : ", sum) − printing the calculated sum of all even numbers.
Conclusion
This is all about calculating the sum of even numbers 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.
- Related Articles
- Haskell Program to calculate the sum of all even numbers
- Swift Program to calculate the sum of all even numbers up to N
- Golang program to calculate the sum of all odd numbers up to N
- Swift Program to calculate the sum of first N even numbers
- Swift Program to calculate the sum of all odd numbers up to N
- Haskell Program to calculate the sum of all odd numbers up to N
- C++ Program to calculate the sum of all odd numbers up to N
- How to Calculate the Sum of Natural Numbers in Golang?
- Golang Program to Print the Sum of all the Positive Numbers and Negative Numbers in a List
- Golang program to calculate the sum of left diagonal matrix
- Golang program to calculate the sum of right diagonal elements
- 8085 program to find the sum of series of even numbers
- Java Program to Calculate the Sum of Natural Numbers
- Swift Program to Calculate the Sum of Natural Numbers
- Kotlin Program to Calculate the Sum of Natural Numbers
