Results 1 to 7 of 7
 09252013, 10:04 PM #1Member
 Join Date
 Oct 2012
 Posts
 18
 Rep Power
 0
need some help doing loops with fibonacci sequence
a bit confused here. i set one variable to 0 and the other to 1. I then ask the user how many elements they wish to only count up to. i guess the way this problem was written up is confusing me even more. im trying to figure out how to finish this. basically if the user only wants the first 5 numbers of the sequence, i then need to print it out. i was going with a for(x,y,z++) but im not sure if it is appropriate in this case. any help would be appreciated
Java Code:import java.util.Scanner; public class FibonacciSequence { /** * @param args */ public static void main(String[] args) { // TODO Autogenerated method stub Scanner input= new Scanner(System.in); long prevNum=0; long newNum=1; System.out.print( "Enter number of elements: "); int element= input.nextInt(); System.out.println(prevNum + " " + newNum +" " + element); for(int i=0;i<=element;i++){ long tempVal
 09252013, 10:18 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,351
 Rep Power
 9
Re: need some help doing loops with fibonacci sequence
The first question I might ask is whether you understand what a Fibonacci sequence is and how to construct one on paper.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09252013, 10:20 PM #3Member
 Join Date
 Oct 2012
 Posts
 18
 Rep Power
 0
 09252013, 10:26 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,351
 Rep Power
 9
Re: need some help doing loops with fibonacci sequence
Exactly! So you need to put that inside a loop. The currentValue is the sum of the previous two. Then you need to update the previous two.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09252013, 10:38 PM #5Member
 Join Date
 Oct 2012
 Posts
 18
 Rep Power
 0
Re: need some help doing loops with fibonacci sequence
sorry jim, you lost me lol
im supposed to have the user choose how long this sequence goes to. if he says he only wants the first 5 numbers, then thats how far itll be. Does that go inside the for loop?
 09252013, 10:43 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,351
 Rep Power
 9
Re: need some help doing loops with fibonacci sequence
The first five numbers just means the first five of the sequence (at least that's my interpretation). But the sequence always starts out with 0 and 1. So the first five would be 0 1 1 2 3. The first eight would be 0 1 1 2 3 5 8 13. The hard part is figuring out how to update the variables inside the loop to continue calculating the next value in the series. It helps to try this on paper with some variables.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09252013, 11:03 PM #7Member
 Join Date
 Oct 2012
 Posts
 18
 Rep Power
 0
Similar Threads

Fibonacci sequence program that only prints the n'th digit in the sequence
By erichfx in forum New To JavaReplies: 1Last Post: 01232013, 07:06 PM 
Fibonacci Sequence Problem
By Zigster in forum New To JavaReplies: 10Last Post: 07062012, 09:54 PM 
Using forloop and arrays to calculate factorials and fibonacci sequence
By baumboards in forum New To JavaReplies: 8Last Post: 03102011, 04:16 AM 
Fibonacci sequence using iterative and recursive method
By baumboards in forum New To JavaReplies: 5Last Post: 03022011, 07:57 PM 
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03252010, 07:59 AM
Bookmarks