Java - Fibonacci Sequence

Questions/Tutorials for any programming language not covered in other sections.
2 posts Page 1 of 1
Contributors
User avatar
Jessica
x Girl Power x
x Girl Power x
Posts: 35
Joined: Thu Feb 21, 2013 1:52 am

Java - Fibonacci Sequence
Jessica
Directions/Requirements:
Add a FibonacciSequence class with a main() method.
 Write a Java program (within the main() method of the FibonacciSequence class) that outputs N
number of elements of the Fibonacci sequence. Use Scanner class to ask the user how many elements in the sequence your program should provide.
 Your program should follow the logic described below in pseudo-code
o Prompt user for a number of sequence elements that the program should display
o Store user’s response in a variable. Hint: use Scanner class
o Declare two numeric variables that will store previous number in the sequence and next
number in the sequence. Make sure you pick the correct data type. Hint: numbers in
the Fibonacci sequence can get pretty large, so using int data type will cause an overflow error. You cannot use double either because you are not working with numbers that have decimal points.
o Initialize the variable that holds previous value to 0.
o Initialize the variable that holds next value to 1.
o Print out the variable that holds previous value. Hint: you need to make sure that all elements of the sequence are displayed on the same line. Which print command do you need to use for that?
o Print out the variable that holds next value.o Write a for loop that will iterate through all values between 0 and the maximum number of elements that user requested. Hint: you already store and display the first two numbers of the sequence by printing the initial values stored in previous element and next element variables. That means that your loop should go from 0 to (required
number – 2).
 Inside the loop, declare a numeric variable that will hold a temporary value. Name it tempVal.
 Store the value of next element into tempVal.
 Set the value of next element to the value of previous element plus the value of next element.
 Set the value of previous element to the value stored in tempVal.
 Print out the value of next element.
 The code inside the loop should follow the pseudo-code below:
Loop
tempVal = next element
next element = previous element + next element
previous element = tempVal
print next element
End Loop
 The final result of your program should be a line of numbers that:
o Follows the Fibonacci sequence logic
o All numbers are displayed on one line
o All numbers are separated by spaces
o Your list has the exact number of elements requested by the user.
Help please?

This is what I have so far, it's totally wrong I know. I really need help.
Code: Select all
public class FibonacciSequence {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.print("Enter number of elements: ");
		int count = in.nextInt();
		long fibo = 0;
		long fiboPrev = 1;
		
		for(int i = 2; i < count; ++i){ 
				long tempVal = fibo; 
				fibo += fiboPrev; 
				fiboPrev = tempVal; } 
		System.out.println(fibo + fiboPrev);
	}

}
Due tomorrow, so please reply before tomorrow midnight EST
User avatar
lolxot
VIP - Donator
VIP - Donator
Posts: 155
Joined: Thu Apr 08, 2010 4:59 pm

Re: Java - Fibonacci Sequence
lolxot
try this
Code: Select all
import java.util.Scanner;
public class FibonacciSequence {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.print("Enter number of elements: ");
		int count = in.nextInt();
		int prevVal = 0, tempVal = 0, nextVal = 1;
		
		System.out.print("0 1 ");
		for (int i = 0; i < count - 2; i++) {
			tempVal = nextVal;
			nextVal = prevVal + tempVal;
			prevVal = tempVal;
			System.out.print(nextVal + " ");
		}
	}
}
2 posts Page 1 of 1
Return to “Misc”