How to to calculate Sum of Geometric Progression in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to to calculate Sum of Geometric Progression using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
##############SOURCE CODE#########################
package main
import (
"fmt"
"math"
)
func main() {
var a, r, n float64
// Read input values from user
fmt.Print("Enter the first term (a): ")
fmt.Scan(&a)
fmt.Print("Enter the common ratio (r): ")
fmt.Scan(&r)
fmt.Print("Enter the number of terms (n): ")
fmt.Scan(&n)
// Calculate the sum of the geometric progression
sum := a * (1 - math.Pow(r, n)) / (1 - r)
// Print the result
fmt.Printf("The sum of the geometric progression is %g\n", sum)
}
##############END OF SOURCE CODE#########################
Below is the explanation for the program:
In this program, we first read the first term a, the common ratio r, and the number of terms n from the user using the fmt.Scan function.
We then calculate the sum of the geometric progression using the formula:
sum = a * (1 - r^n) / (1 - r)
where a is the first term, r is the common ratio, and n is the number of terms.
Finally, we print the result using the fmt.Printf function, which allows us to format the output string using the %g verb to print the result in a compact form.
#go #goprogramming #golang #golangtutorial #golanguage