Reputation: 39
i am programming a Fibonacci series, every thing works fine except that i am failing to limit the out put accoring to the while condition (While output < maxOutput) is failing to limit my output to the value entered by a user. Where am i getting my while condition wrong.See my code below
import java.util.Scanner;
public class Fibonacci {
private static int fib(int prev_Total, int current_Num) {
return current_Num + prev_Total; // return sum of the two
}
public static void main(String[] args){
int f_Num, s_Num, maxOutput;
Scanner maxNum = new Scanner(System.in);
System.out.println("Enter the first value: ");
f_Num = maxNum.nextInt();
System.out.println("Enter the second value: ");
s_Num = maxNum.nextInt();
System.out.println("Enter the maximum value of the series: ");
maxOutput = maxNum.nextInt();
System.out.println(f_Num); // print the first value by default
System.out.println(s_Num); // print the second value by default
int prevTotal = f_Num; // initialise prevTotal
int currentNum = s_Num; // initialise currentTotal
int output = 0; // initialise output
while (output < maxOutput) {
output = fib(currentNum, prevTotal); // assign the result of first two numbers added together to first output
prevTotal = currentNum; // update prevTotal (currentNum becomes our new prevTotal)
currentNum = output; // update currentNum (output becomes our new currentNum)
System.out.println(output); // print output
}
}
}
Upvotes: 0
Views: 58
Reputation: 551
This should work:
while (true) {
// assign the result of first two numbers added together to first output
output = fib(currentNum, prevTotal);
// update prevTotal (currentNum becomes our new prevTotal)
prevTotal = currentNum;
currentNum = output;
if (output > maxOutput) {
break;
}
// print output
System.out.println(output);
}
...hope I helped you!
Upvotes: 1
Reputation: 3649
Your for loop will should be somewhat like:
while (output < maxOutput){
System.out.println(output); // print output
output = fib(currentNum, prevTotal); // assign the result of first two numbers added together to first output
prevTotal = currentNum; // update prevTotal (currentNum becomes our new prevTotal)
currentNum = output; // update currentNum (output becomes our new currentNum)
}
EDIT: A slight edit in logic will work out
int output = 0;
int prevTotal = s_Num; // initialise prevTotal
int currentNum = output = fib(f_Num, prevTotal); // initialise currentTotal with output
while (output < maxOutput){
System.out.println(output); // print output
output = fib(currentNum, prevTotal); // assign the result of first two numbers added together to first output
prevTotal = currentNum; // update prevTotal (currentNum becomes our new prevTotal)
currentNum = output; // update currentNum (output becomes our new currentNum)
}
Upvotes: 0
Reputation: 5168
Reverse the loop, use a do .. while() it will work.
Edited:
Put the System.out.println(output); in the first line of the loop, after the while, like:
while (output < maxOutput){
System.out.println(output); // print output
...
Upvotes: 0