This Go program uses the for loop to iterate from 1 to n and finds the sum and average of natural numbers. Inside the for loop, we add each number to the total, and outside the loop, we calculated the average.

package main import "fmt" func main() { var num, i int fmt.Print("\nEnter the Maximum Natural Number = ") fmt.Scanln(&num) total := 0 for i = 1; i <= num; i++ { total = total + i } average := total / num fmt.Println("The Sum of Natural Numbers from 1 to ", num, " = ", total) fmt.Println("The Average of Natural Numbers from 1 to ", num, " = ", average) }

## Golang Program to Sum and Average of Natural Numbers

In this Golang program, we declared a new function that returns the sum of natural numbers from 1 to N. In that function, we used the mathematical formula n(n+1)/2 to get the sum.

package main import "fmt" func sumAvg(num int) int { if num == 0 { return num } else { return (num * (num + 1) / 2) } } func main() { var num int fmt.Print("\nEnter the Maximum Natural Number = ") fmt.Scanln(&num) total := sumAvg(num) average := total / num fmt.Println("The Sum of Natural Numbers from 1 to ", num, " = ", total) fmt.Println("The Average of Natural Numbers from 1 to ", num, " = ", average) }

In this Go program, we are recursively calling the sumAvg function with the updated value. The main part of this Go program to find Sum and Average of Natural Numbers is (num + sumAvg(num-1)).

package main import "fmt" func sumAvg(num int) int { if num == 0 { return num } else { return (num + sumAvg(num-1)) } } func main() { var num int fmt.Print("\nEnter the Maximum Natural Number = ") fmt.Scanln(&num) total := sumAvg(num) average := total / num fmt.Println("The Sum of Natural Numbers from 1 to ", num, " = ", total) fmt.Println("The Average of Natural Numbers from 1 to ", num, " = ", average) }