0

I need help with my java-program. This program is supposed to ask for the highest value fibonacci can have, and print out the number of series up to that value, but it doesn't work. Any suggestions?

import java.util.Scanner;

public class Fibonacci {
    public static void main (String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("The largest number fibonacci can be: ");
        int number = in.nextInt();
        if (number < 0){
            System.out.println("Wrong! Max-value has to be at least 0.");
        }
        int i;
        int f0 = 0;
        int f1 = 1;
        int fn;
        int value=0;
        for (i = 0; i<=value; i++){
            fn = f0 + f1;
            System.out.println("Fibonacci-number " + i + " = " + f0);
            f0 = f1;
            f1 = fn;
            value = number - f0;
        }
    }
}

If i put in number = 12, the program is supposed to print:

fibonacci-number 0 = 0 

...

fibonnaci-number 12 = 144
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • 1
    "[...] but it doesn't work" => This is not an adequate problem description. Please tell us what you expect, and what happens instead. As your program asks the user for input, please tell us also what input values you are testing with. And - last but not least - start to learn how to [debug a Java program](http://www.vogella.com/tutorials/EclipseDebugging/article.html). – Seelenvirtuose Sep 10 '16 at 11:12
  • I don't understand your `for` loop, `for (i = 0; i<=value; i++)`, it will only run once so what is the point of it? – smoggers Sep 10 '16 at 11:12
  • Possible duplicate of [In java, how would I find the nth Fibonacci number?](http://stackoverflow.com/questions/13021102/in-java-how-would-i-find-the-nth-fibonacci-number) – Alexey Soshin Sep 10 '16 at 11:15
  • @smoggers look more closely, you'll see that 'value' is changed later. That said, I'm not sure this for loop is correctly specified. Isn't it supposed to continue running until value reaches a certain number? – Perry Monschau Sep 10 '16 at 11:17

4 Answers4

0

Is this what you are looking for?

    public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int f0 = 0;
int f1 = 1;
int fn = 0;

System.out.println("The largest number fibonnaci can be: ");
System.out.println("Input your number: ");
int number = in.nextInt();

if (number < 0){
    System.out.println("Wrong! Max-value has to be at least 0.");
} else {
    System.out.println(f0);
    System.out.println(f1);
    while (fn < number){
        fn = f0 + f1;
        f0 = f1;
        f1 = fn;
        if (fn < number){
            System.out.println(fn);
        }
    }
}
}
0

Just change the loop to compare value of increment-er (i) to 'number ' variable

for (i = 0; i<=number; i++){
//.........
}

Also use double instead of int if you want to print higher number in the series correctly.

There is no use of the 'value' variable.

Also, the phrase 'the highest value fibonacci can have' is misleading.You have mentioned number denotes number of terms in the series in your example.

If you want 'number' to be the highest value in the series, use following approach,

do{

        fn = f0 + f1;
        System.out.println("Fibonnaci-tall " + i + " = " + f0);
        f0 = f1;
        f1 = fn;
        i++;

}while(f0<=number);
Sarit Adhikari
  • 1,344
  • 2
  • 16
  • 28
0

enter image description here

Use this program, it will solve your query.

  • Thank you for adding another answer in which you copy-paste the code, instead of the snapshot. But please remove this one. Thanks. – lrnzcig Sep 11 '16 at 13:39
0

Try this...It works!

 public static void main (String[] args){
    Scanner in = new Scanner(System.in);
    System.out.println("The largest number fibonacci can be: ");
    int number = in.nextInt();
    if (number < 0){
        System.out.println("Wrong! Max-value has to be at least 0.");
    }
    else{
    int i=0;
    int f0 = 0;
    int f1 = 1;
    int fn;
    int value=0;
   do{
    //for (i = 0; i<=value; i++){
        fn = f0 + f1;
        System.out.println("Fibonacci-number " + i + " = " + f0);
        f0 = f1;
        f1 = fn;
        value = number - f0;
        i++;
    }while(f0<=number);
}//else
}