I am Looking for you guys to look over my code for "The Fibonnaci series" and tell me what you think, have i added to much code and do i need the if statement inside the for loop?
Any newbies want to use it work away!
Here is the Question and the code :
/* Question: The Fibonnaci series is as follows;
0, 1, 1, 2, 3, 5, 8, 13, 21, 34.......
After the first 2 numbers, each number is the sum of the two previous numbers.
Write a program to compute the nth term in the series. Have your program to print
the series to the nth term. */
// Gman
// 25/11/2011
public class FibonacciQ5Part2
{
public static void main(String[] args)
{
// declare variable
int termRequired = 11;
int nthTerm = 0;
int twoPrevious = 0;
int onePrevious = 1;
// calculate fibonnaci
for (int i = 2; i < termRequired; i++)
{
// if statement so i can get the zero and other 1 to print on screen
if (nthTerm < 2)
{
System.out.println("Current:\t" + nthTerm + " ");
}
nthTerm = onePrevious + twoPrevious;
System.out.println("Current:\t" + nthTerm + " ");
twoPrevious = onePrevious;
onePrevious = nthTerm;
}
// output / display on screen
System.out.println("Position " + termRequired + " has a value of " + nthTerm);
}
}
Don't forget i am only new to java and only learned for loop this week! Go easy!!
Gman
Edited by Gman, 25 November 2011 - 02:04 PM.
spell check :)


Sign In
Create Account


Back to top









