- 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
How to Calculate the Sum of Natural Numbers in Golang?
In this tutorial, we are going to see how to find the sum of Natural Numbers in Golang. To do that we have two ways one using the formula itself and the other is to use a for loop we will explore both ways in this tutorial.
Formula
Sum = ( N * ( N + 1))/2 N = The value of the natural number till which you want to find the sum.
Explanation
Let's find the sum of the first 6 natural numbers using the above formula.
Sum of first 6 natural numbers = 1 + 2 + 3 + 4 + 5 + 6 = ( N * ( N + 1)) / 2 = ( 6 * ( 6 + 1 )) / 2 = ( 6 * 7) / 2 = 42 / 2 = 21
Finding the sum of the Natural number using the formula
Algorithm
Step 1 − Declaring the variable N that is storing the number till which we have to find the sum and also and also the answer variable to store the final result.
Step 2 − Initializing the variable N.
Step 3 − Calling the sumOfNNaturalNumbers() function that is finding the sum using the formula mentioned above.
Step 4 − Printing the result.
Time Complexity: O(1) Space Complexity: O(1)
Example 1
In this example, we are going to find the sum of N natural numbers using the above formulae.
package main import "fmt" // fmt package provides the function to print anything // defining the function with a parameter of int32 // type and have a return type int32 func sumOfNNaturalNumbers(N int32) int32 { // declaring the variable sum of int32 type // that will store the sum of N Natural numbers var sum int32 // finding the sum using the formula and storing // into the variable sum = (N * (N + 1)) / 2 // returning the sum return sum } func main() { // declaring the variable N of int32 type till which we // have to find the sum of Natural numbers and a variable // answer that will store the sum var N, answer int32 // initializing the variable N N = 10 fmt.Println("Program to find the sum of the Natural number using the formula.") // calling the sumOfNNaturalNumbers() function and storing // the result in the answer variable answer = sumOfNNaturalNumbers(N) fmt.Println("The sum of", N, "natural numbers is", answer) }
Output
Program to find the sum of the Natural number using the formula. The sum of 10 natural numbers is 55
Finding the sum of the Natural number using the for loop
Algorithm
Step 1 − Declaring the variable N that is storing the number till which we have to find the sum and also and also the answer variable to store the final result.
Step 2 − Initializing the variable N.
Step 3 − Calling the sumOfNNaturalNumbers() function that is finding the sum using the for loop.
Step 4 − Printing the result.
Time Complexity: O(N) Space Complexity: O(1)
Example 2
In this example, we are going to find the sum of N natural numbers using the for loop.
package main import "fmt" // fmt package provides the function to print anything // defining the function with a parameter of int32 // type and have a return type int32 func sumOfNNaturalNumbers(N int32) int32 { // declaring the variable sum of int32 type // that will store the sum of N Natural numbers // and a variable iterator that we will use in for loop var iterator, sum int32 // initializing the sum variable with 0 sum = 0 // running a for loop from 1 till N for iterator = 1; iterator <= N; iterator = iterator + 1 { // adding each natural number in the sum sum = sum + iterator } // returning the sum return sum } func main() { // declaring the variable N of int32 type till which we // have to find the sum of Natural numbers and a variable // answer that will store the sum var N, answer int32 // initializing the variable N N = 10 fmt.Println("Program to find the sum of the Natural number using the for loop.") // calling the sumOfNNaturalNumbers() function and storing // the result in the answer variable answer = sumOfNNaturalNumbers(N) fmt.Println("The sum of", N, "natural numbers is", answer) }
Output
Program to find the sum of the Natural number using the for loop. The sum of 10 natural numbers is 55
Conclusion
These are the two ways to find the sum of Natural numbers in Golang. The first way is much better in terms of time complexity. To learn more about go you can explore these tutorials.
- Related Articles
- 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
- Haskell program to calculate the sum of natural numbers
- C++ Program to Calculate Sum of Natural Numbers
- Golang Program to Find the Sum of Natural Numbers using Recursion
- Golang program to calculate the sum of all even numbers
- PHP program to calculate the sum of square of first n natural numbers
- Swift Program to Calculate Cube Sum of First n Natural Numbers
- Java Program to calculate Sum of squares of first n natural numbers
- Golang program to calculate the sum of all odd numbers up to N
- How to Find the Sum of Natural Numbers using Python?
- How to Find Sum of Natural Numbers Using Recursion in Python?
- Sum of sum of first n natural numbers in C++
- Java program to find the sum of n natural numbers
