--Originally published at my programming blog
Today I had to do the quiz #8 which was to create a function where I needed to calculate the nth number in the Fibonacci sequence. I had a lot of trouble with this quiz!!!!! I didn’t go to class, so I couldn’t ask to my classmates or to Ken, so I searched for a lot of examples and tutorials of different codes for this program and with what I understood I created my own program.
What I did was:
- In my function I only declared one value x, which will be the number the user will enter.
- Then I made an if that if x was equal to something less than 2, then the fibonacci number would be x.
- Then I declared 3 values, n1, n2 and n, where n1 is 0 (the first fibonacci number) and n2 is 1 (the second fibonacci number) and n the total value of the fibonacci number we will calculate.
- Then I did a for loop and as you can see on the code I equal n to n1 + n2, n1 equals n2, and n2 equals to n (This are the operations to obtain the fibonacci number).
- And the return value was n.
- The in my int main I asked the user for the value of a number in the fibonacci sequence which is the integer value.
- Then to print the number I called the function but I changed the inside of the parenthesis to the int value so that the fibonacci number can be calculated based on the value the user entered.
- And that’s it.
I hope you understand it! If not here are some examples of different codes and a tutorial that can give you an idea to create your own code. Also check the work of our classmates in
Continue reading "This is getting hard…."