Different ways of Sum of natural numbers in Golang Code exampls

This post cover three programs to find the sum of natural numbers

  • Sum of first n natural numbers entered by the user
  • Sum of first n natural numbers using for loop
  • Sum of first n natural numbers Recursive function

Natural numbers are positive integers or whole numbers which start from 0,1, 2 … n.

These are also called non-positive numbers. This post is about calculating the sum of natural numbers from 1 to n where n is read from the console by the user.

To understand these programs, You should have an understanding of the following golang features.

How to Sum of first n natural numbers Entered by user in Golang

The below program takes an input number from the command line console from a user and calculates the sum of natural numbers up to a given number.

package main

import (
    "fmt"
)

func main() {
    //reading an multiple input string
    var number int
    fmt.Print("Please enter number:")
    fmt.Scanln( & number)
    sumResult: = 0
    for i: = 0;
    i <= number;
    i++{
        sumResult += i
    }
    fmt.Printf("Sum of  %d numbers is  %d", number, sumResult)
}

Output:

Please enter number:10
The Sum of  10 numbers is  55

The above program reads input from the user using the Scanln function, stores the number in a variable number.
Using for loop, iterate and calculate the values from 0 up to n
Finally, a sum prints to the console.

How to do Sum of natural numbers using for loop n Golang

In this program, Instead of taking input from the user, the variable value is fixed, Like the above program, for loop is used to calculate up to a given number.
Finally, print value to the console.

package main

import (
    "fmt"
)

func main() {
    max: = 10
    sumResult: = 0
    for i: = 0;i <= max;i++{
        sumResult += i
    }
    fmt.Printf("Sum of  %d numbers is  %d", max, sumResult)
}

Output is

The Sum of  10 numbers is  55

How to calculate Sum of natural numbers using recursion function in Golang

This example calculates the sum of natural numbers using a recursive function.

package main

import (
    "fmt"
)

func sumNatural(numb int) int {
    if numb != 0 {
        return numb + sumNatural(numb - 1)
    } else {
        return numb
    }
}

func main() {
    number: = 10
    fmt.Printf("Sum of  %d numbers is  %d", number, sumNatural(number))
}

Output:

Sum of  10 numbers is  55

In the above program, sumNatural recursive function is declared. It calls in the main function initially first using the sumNatural(10) function.

The number 10 is added as the result of sumNatural(9). The next function call will be fired from sumNatural which again calls sumNatural(8) and add this result. This sequence of the process will continue until numb is equal to 0.

recursive call execution will be stopped When numb is zero and finally return the result to the main function

Conclusion

In this tutorial, I Learned the sum of natural numbers entered by the user, using for loop and recursive function.