Determining the Fibonacci sequence is easy enough to figure out:
int num = 0;
int num2 = 1;
int loop;
int fibonacci;
System.out.print(num2);
for (loop = 1; loop <= 10; loop ++)
{
fibonacci = num + num2;
num = num2;
num2 = fibonacci;
System.out.print(" " + fibonacci);
}
My problem lies with trying to pinpoint the value for a specified N. As in, If I want to find the 6th element in the sequence, which is 8, how would I find that number, and only that number?
In your code,
numstarts as the 0th Fibonacci number, andnum1as the 1st. So to find the nth, you have to iterate the stepntimes:and only print it when you’ve finished.
When the loop counter
loophas the valuek,numholds the kth Fibonacci number andnum2the (k+1)th.