-1

Created a program that would input a number and provides its equivalent fibonacci number as an output. But there's something wrong with my code :c

import java.util.Scanner;
public class Fibonacci {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        int prev, next, num, sum = 0, n;
        prev=next=1;

        System.out.print("Input number: ");
        num = in.nextInt();

        if ((num == 1) || (num ==2))
            System.out.println(prev);

        else {
            for( n=1; n<=prev; n++ ) {
                sum = prev + next;
                prev = next;
                next = sum;
            }

            System.out.println(sum);
        }

    }
}

Something's wrong I just can't see. Help? :c

  • 5
    "Wrong" is not clear, please explain what's wrong –  Feb 21 '14 at 19:44
  • 1
    What do you mean by "its equivalent fibonacci number"? – dub stylee Feb 21 '14 at 19:44
  • Time to learn how to use a `debugger`. Better to learn how to debug now before you start writing longer and more complex programs. – Tdorno Feb 21 '14 at 19:48
  • You might want to move this code into a method as well. Not sure if you've been exposed to recursion yet, but that's the best (read: most concise) way to solve this problem. – turbo Feb 21 '14 at 19:53
  • 1
    Just to mention, as well as the [closed formula](http://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression) `O(1)` version (subject to precision problems for large numbers, will require `BigDecimals` with a lot of decimal places for those), there are variations of the algorithm which costs `O(log(n))`[(java example)](http://stackoverflow.com/a/15093963/664577). – Anthony Accioly Feb 21 '14 at 20:10

3 Answers3

2

You're stopping the loop when n reaches beyond prev, so you aren't getting the correct number. Stop it when you've passed num instead:

for(n = 1; n <= num; n++) {

Example run:

Input number: 6
21
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

Change your for loop to this:

for( n=2; n<num; n++ )

That'll solve your problem.

Explanation: Since you've already determined that 1 or 2 will give you a 1, start your loop @ 2. Loop through until n becomes larger than the entered number. This will solve the problem you were having.

Opzoleet
  • 38
  • 1
  • 8
0

If you want to get the fiboniki number times the user has given input then you need to iterate in for loop.

 for( n=1; n<=num; n++ ) {
                    sum = prev + next;
                    prev = next;
                    next = sum;
                    System.out.println(sum);
                }

Also to print the number you need to put it in the loop.

Kick
  • 4,823
  • 3
  • 22
  • 29
  • 3
    Instead of doing homework for people, why not explain the process of working through the problem and debugging code? – Kevin Workman Feb 21 '14 at 19:46
  • kk sir @KevinWorkman.It will be better if you wait for some time before downvote the answer – Kick Feb 21 '14 at 19:50