Golang Program to Find the Sum of Natural Numbers using Recursion


In this tutorial, we are going to learn how to find the sum of Natural Numbers using Recursion in Golang.

A Recursion is where a function calls itself by direct or indirect means. Every recursive function has a base case or base condition which is the final executable statement in recursion and halts further calls.

Below are two examples showing the two different type of recursion: direct and indirect.

Find the Sum of Natural numbers by Direct Recursion Method

Syntax

func recursion() {
   recursion()
}
func main() {
   recursion();
}

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Create the function naturalnum()

  • Step 3 − We will use an if...else conditional statement

  • Step 4 − Start the function main()

  • Step 5 − Initialize the integer variable

  • Step 6 − Call the function naturalnum()

  • Step 7 − Print the result on the screen using fmt.Printf()

Example

Golang Program code to Find the Sum of Natural Numbers using Recursion by using Direct Recursion Method.

// GOLANG PROGRAM TO FIND THE SUM OF // NATURAL NUMBERS USING RECURSION package main // fmt package provides the function to print anything import ( "fmt" ) // defining the function with a parameter of int // type and have a return type int func naturalnum(num int) int { // this is the base condition // if num is not equal 0 if num != 0 { // recursive call to itself return num + naturalnum(num-1) } else { // function will return num and end the recursive function return num } } // start the main() function func main() { fmt.Println("Golang Program to find the sum of natural numbers using recursion") // initializing the integer variable number := 10 // calling the naturalnum() function fmt.Printf("Sum of all the natural numbers till %d is %d", number, naturalnum(number)) // Prints the final result }

Output

Golang Program to find the sum of natural numbers using recursion
Sum of all the natural numbers till 10 is 55

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt

  • Next we create a function naturalnum () to find sum of natural numbers using recursion technique

  • We declare the integer variable num

  • We will use an if...else conditional statement which allows you to execute one block of code if the specified condition is true and another block of code if the condition is false.

  • If num is not equal 0, then recursive call to the function itself

  • Else function will return num and end the recursive function

  • Now start the function main()

  • Next initialize the integer variable number

  • Now calling the naturalnum() function

  • And finally printing the result on the screen using fmt.Printf().

Find the Sum of Natural Numbers by Indirect Recursion Method

Syntax

func recursion_1() {
   recursion_2()
}
func recursion_2(){
   recursion_1()
}
func main() {
   recursion_1();
}

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Create the function natural1 ()

  • Step 3 − We will use an if...else conditional statement

  • Step 4 − Create the function natural2()

  • Step 5 − Recursive call to the function natural1() indirectly

  • Step 6 − Start the function main()

  • Step 7 − Initialize the integer variable num

  • Step 8 − Call the function natural1()

  • Step 9 − Print the result on the screen using fmt.Printf()

Example

Golang program code to find the sum of natural numbers using recursion by using indirect recursion method

// GOLANG PROGRAM TO FIND THE SUM OF // NATURAL NUMBERS USING RECURSION package main // fmt package provides the function to print anything import ( "fmt" ) // defining the function with a parameter of int // type and have a return type int func natural1(num int) int { // this is the base condition // if num is not equal 0 if num != 0 { // recursive call to the second function return num + natural2(num-1) } else { // function will return num and end the recursive function return num } } func natural2(num int) int { // this is the base condition // if num is not equal 0 if num != 0 { // recursive call to the first function indirectly return num + natural1(num-1) } else { // function will return num and end the recursive function return num } } // start the main() function func main() { fmt.Println("Golang Program to find the sum of natural numbers using recursion") // initializing the integer variable number := 50 // calling the natural1() function fmt.Printf("Sum of all natural numbers till %d is %d", number, natural1(number)) // Prints the final result }

Output

Golang Program to find the sum of natural numbers using recursion
Sum of all natural numbers till 50 is 1275

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt

  • Next we create a function natural1() to find sum of natural numbers using recursion technique

  • We will use an if conditional statement which allows you to execute one block of code if the specified condition is true and then recursive call to the function natural2 ()

  • Next we create a function natural2().Here recursive function call to the first function is made which calls the first function natural () indirectly

  • Now start the function main().GO program execution starts with the function main()

  • Next we call the natural1() function to find sum of natural numbers.

  • And finally printing the result on the screen using in-built function fmt.Printf().This function is defined under the fmt package and it helps to write standard output.

Conclusion

In the above two examples we have successfully compiled and executed the Golang program code to find the sum of natural numbers using recursion technique. We have shown direct and indirect type of recursion method. In the first example we have shown direct recursion method and in the second example we have shown indirect recursion method.

Updated on: 25-Oct-2022

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements