{

Golang example - Program to calculate Sum of natural numbers


Golang example calculate Sum of natural numbers

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.

Example program 1- Sum of first n natural numbers Entered by user

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.

Example program 2- Sum of natural numbers using for loop

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

Example program 3- Recursion function Sum of natural numbers

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.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.