java - Print fibonacci up to user entered number -



java - Print fibonacci up to user entered number -

i'm trying print fibonacci series whatever number user types in. problem code ends printing amount of numbers. if user enters 100, want code stop @ 100, code prints 100 numbers. also, i'm supposed have printf , while statement. don't know how utilize printf.

package l12; import java.util.scanner; public class l12 { /** * @param args command line arguments */ public static void main(string[] args) { scanner console = new scanner(system.in); system.out.println("enter integer: "); int n = console.nextint(); system.out.println("the fibonacci numbers less " + n + " are: "); for(int i=1; i<=n; i++){ system.out.print(fibonacci(i) +" "); } } public static int fibonacci(int n){ if(n == 1 || n == 2){ homecoming 1; } int f1=1; int f2=1; int fibonacci=1; for(int i= 3; i<= n; i++){ { fibonacci = f1 + f2; f1 = f2; f2 = fibonacci; } while (fibonacci <= n); } homecoming fibonacci; } }

a simple way of doing :

public static void main(string [] args){ scanner console = new scanner(system.in); system.out.println("enter integer: "); int n = console.nextint(); if(n>1) { system.out.println("the fibonacci numbers less " + n + " are: "); system.out.print("1, 1, "); int f2=1; int fibonacci=1; for(int i=1; i+f2 < n;){ fibonacci = + f2; = f2; f2 = fibonacci; system.out.print(fibonacci+", "); } } else { system.out.println("there no numbers less " + n + " in series"); } }

java

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -