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.

Updated on: 02-Sep-2022

898 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements