Skip to content

Instantly share code, notes, and snippets.

@kenechiokolo
Created March 23, 2015 12:24
Show Gist options
  • Save kenechiokolo/46e77789556c69c34eca to your computer and use it in GitHub Desktop.
Save kenechiokolo/46e77789556c69c34eca to your computer and use it in GitHub Desktop.
CS106A Section Assignment 2: The Fibonacci Sequence
import acm.program.*;
// this program prints the fibonacci sequence
public class sectionAssignment_2 extends ConsoleProgram {
// specifies max value for term in fibonacci sequence
private static final int MAX_TERM_VALUE = 10000;
public void run() {
introduction();
fibonacciNumbers();
}
// prints the program introduction line
private void introduction() {
println("This program lists the fibonacci numbers.");
}
/* this method calculates and prints the fibonacci sequence
* starting at 0 and while the current term < MAX_TERM_VALUE
*/
private void fibonacciNumbers() {
while (term < MAX_TERM_VALUE) {
println(""+pTerm+"");
term = term + pTerm;
pTerm = term - pTerm;
}
println(""+pTerm+"");
}
//pTerm specifies the value of the previous term in the fibonacci sequence
int pTerm = 0;
//term specifies the value of the current term in the fibonacci sequence
int term = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment