Skip to content

Instantly share code, notes, and snippets.

@jonathan-fielding
Last active February 10, 2021 20:56
Show Gist options
  • Save jonathan-fielding/9720d9cb2c7d0a58da3bd799aa265c27 to your computer and use it in GitHub Desktop.
Save jonathan-fielding/9720d9cb2c7d0a58da3bd799aa265c27 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
var nth int
// Print our request to the user
fmt.Println("Enter Fibonacci number you require?")
// Request an number from the user and store it
// in the nth variable
fmt.Scanf("%d", &nth)
// Define our starting point
var lastNumber int = 0
var currentNumber int = 1
// Define a temporary store of the value for
// when we are shifting along the sequence
var nextNumber int = 0
// Loop starting at 1 through each of the fibonacci
// numbers until we find the one we want
for i := 1; i < nth; i++ {
nextNumber = lastNumber + currentNumber
lastNumber = currentNumber
currentNumber = nextNumber
}
// Output the fibonacci number we want
fmt.Println(currentNumber)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment