- 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 Find the Sum of N Numbers using Recursion
In this tutorial, we are going to learn how to find the sum of N Numbers using Recursion in Golang programming language.
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 N Numbers by Direct Recursion Method
Syntax
Syntax for direct recursion func recursion() { recursion() } func main() { recursion(); }
Algorithm
Step 1 − Import the package fmt
Step 2 − Create the function sum()
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 sum()
Step 7 − Print the result on the screen using fmt.Printf()
Example
Golang Program Code to Find the Sum of N Numbers using Recursion by using Direct Recursion Method.
// GOLANG PROGRAM TO FIND THE SUM OF // N 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 sum(n int) int { // this is the base condition // if n is equal to 0 if n == 0 { // function will return 0 and end the recursive function return 0 } else { // recursive call to itself return n + sum(n - 1) } } // start the main() function func main() { // Declare and initialize integer variable number := 25 fmt.Println("Sum of first n numbers using recursion in Golang Program") fmt.Printf("Sum of the first %d numbers is %d", number, sum(number)) // Prints the final result }
Output
Sum of first n numbers using recursion in Golang Program Sum of the first 25 numbers is 325
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 sum () to find sum of n numbers using recursion technique
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 n is equal to 0, function will return 0 and end the recursive function
Else function will recursive call to the function itself and return (n + sum(n-1))
Now start the function main()
Next initialize the integer variable number
Now calling the sum() function
And finally printing the result on the screen using fmt.Printf().
Find the Sum of N 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 sum ()
Step 3 − We will use an if...else conditional statement
Step 4 − Create the function sumofn ()
Step 5 − Recursive call to the function sum() indirectly
Step 6 − Start the function main()
Step 7 − Initialize the integer variable number
Step 8 − Call the function sum()
Step 9 − Print the result on the screen using fmt.Printf()
Example
Golang Program Code to Find the Sum of N Numbers using Recursion by using Indirect Recursion Method.
// GOLANG PROGRAM TO FIND THE SUM OF // N 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 sum(n int) int { // this is the base condition // if num is equal to 0 if n == 0 { // function will return 0 and end the recursive function return 0 } else { // recursive call to sumofn () return n + sumofn(n - 1) } } // defining the function with a parameter of int // type and have a return type int func sumofn(n int) int { // this is the base condition // if num is equal to 0 if n == 0 { // function will return 0 and end the recursive function return 0 } else { // recursive call to function sum () return n + sum(n - 1) } } // start the main() function func main() { // Declare and initialize integer variable number := 55 fmt.Println("Sum of first n numbers using recursion in Golang Program") // calling the function sum() fmt.Printf("Sum of first %d numbers is %d", number, sum(number)) // Prints the final result }
Output
Sum of first n numbers using recursion in Golang Program Sum of first 55 numbers is 1540
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
Next we create a function sum () to find sum of n numbers using recursion technique
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 n is equal to 0, function will return 0 and end the recursive function
Else function will recursive call to the function sumofn() and return (n + sumofn(n-1))
Next we create a function sumofn () and similarly like the above function make a recursive call to the function sum() and return (n + sum(n-1))
Now start the function main()
Next initialize the integer variable number
Now calling the first function sum()
And finally printing the result on the screen using fmt.Printf().
Conclusion
In the above two examples we have successfully compiled and executed the Golang program code to find the sum of n number 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.
- Related Articles
- Golang Program to Find the Sum of Natural Numbers using Recursion
- Java Program to Find Sum of N Numbers Using Recursion
- Haskell Program to Find Sum of N Numbers Using Recursion
- Golang Program to Find the Product of Two Numbers Using Recursion
- Java Program to Find the Sum of Natural Numbers using Recursion
- Haskell Program to Find the Sum of Natural Numbers using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- Golang Program to Find G.C.D Using Recursion
- How to find the LCM of two given numbers using Recursion in Golang?
- How to find the GCD of Two given numbers using Recursion in Golang?
- How to Find Sum of Natural Numbers Using Recursion in Python?
- C++ Program to Find Fibonacci Numbers using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- Java Program to Find the Product of Two Numbers Using Recursion
- Haskell Program to Find the Product of Two Numbers Using Recursion
