java - While loop condition is not giving anticipated results -


i programming fibonacci series, every thing works fine except failing limit out put accoring while condition (while output < maxoutput) failing limit output value entered user. getting while condition wrong.see 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 2     }      public static void main(string[] args){         int f_num, s_num, maxoutput;          scanner maxnum = new scanner(system.in);          system.out.println("enter first value: ");         f_num = maxnum.nextint();          system.out.println("enter second value: ");         s_num = maxnum.nextint();          system.out.println("enter maximum value of series: ");         maxoutput = maxnum.nextint();           system.out.println(f_num);  // print first value default          system.out.println(s_num); // print second value 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 result of first 2 numbers added 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           }     } }    

this should work:

while (true) {     // assign result of first 2 numbers added 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 helped you!


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -