java - nth number in the Fibonacci sequence -



java - nth number in the Fibonacci sequence -

i not know how print, in fibonacci sequence number (nth number). bold text i'm having problem , have utilize while loop.

please input number analysis >> 1 1 fibonacci number whose order in sequence both 2 , 3.

please input number analysis >> 56 55 not fibonacci number. however 56 between 11 , 12.

here code

import java.util.scanner; public class while { public static void main(string[] args) { system.out.println("welcome fibonacci sequence detector\n\n"); scanner in = new scanner(system.in); system.out.print("please input number analysis: "); int input = in.nextint(); int fib = 0; int fib1 = 1; int n; while(true) { n=fib+fib1; if(input == fib1) { fib = -1; break; } if(input>fib1 && input < n) { break; } fib = fib1; fib1=n; } if (fib == -1 || input == 0) system.out.println(input+" fibonacci number order in sequence " ); else system.out.println(input+ " not fibonacci number" ); } }

the easiest way can think of have counter variable increment every time through.

while(true) { count++; ... } ... system.out.println(input+" fibonacci number order in sequence "+count);

as side note, there reason you're using while(true)? there's way track status want stop looping. (i taught while(true) isn't wrong, is.) :)

java while-loop

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -