user2177177
user2177177

Reputation: 21

Fibonacci Recursion in Java - No For Loops?

I don't understand what I should be doing. My professor wants us to create a Fibonacci sequence using recursion. No for loops are allowed, and I (being an amateur) don't know how to create a String of say, 6 numbers in sequence.

Here are his directions: "Using recursion, create a method that returns a String containing a Fibonacci sequence. Take in an integer to determine how many values of the sequence you should return."

This is what I have thus far...

import java.util.*;

public class fibo {

  public final static int n = 0;
  public static String s = "";

  public static void main(String[] args) {

    Scanner scn = new Scanner (System.in);

    System.out.println("Please put in a number.");

    int n = scn.nextInt();

    s = Integer.toString(n);

    System.out.println(n+ ": " + fibonacci(n));
  }

  public static int fibonacci(int n) {  

    if(n <= 1)
      return n;

    else       
      return fibonacci(n - 1) + fibonacci(n - 2);
  }
}

In addition to that, I feel like a lot of it is really inefficient and messy. Can someone really explain and help me with what I should be doing?

Upvotes: 0

Views: 4994

Answers (4)

vtboyarc
vtboyarc

Reputation: 33

This should work just fine:


public class Fibonacci {

  public static void main(String[] args) {
    System.out.println(Fib(3));

  }

  // returns the next number in the Fibonacci sequence
  public static int Fib(int n) {
    if (n < 2) {
      return n;
    } else {
      return Fib(n - 1) + Fib(n - 2);
    }
  }
}

Upvotes: 1

Ahmed Nasrullah
Ahmed Nasrullah

Reputation: 1

Here is Code which works good, without unnecessary Code.

import java.util.*;

public class Fibonaccis {



  public static void main(String[] args) {

    Scanner scn = new Scanner (System.in);

    System.out.println("Lägg in ett nummer.");

    int n = scn.nextInt();



    System.out.println(n+ ": " + fibonacci(n));
  }

// TODO Auto-generated method stub

  public static int fibonacci(int n) {  

    if(n <= 1)
      return n;

    else       
      return fibonacci(n - 1) + fibonacci(n - 2);
  }
}

Upvotes: 0

Bhushankumar Lilapara
Bhushankumar Lilapara

Reputation: 780

This code showing Fibonacci till 8 without any loop...

public class FinnonnacciDemo2 {

    static int no = 0, n = 8;

    public static void main(String[] args) {
        // This will print series till 8
        fib(0, 1);
    }

    public static void fib(int a, int b) {
        // Terminating condition.
        if (a >= n) {
            return;
        }

        else {
            System.out.print("\t" + no);
            no = a + b;
            a = b;
            b = no;
            fib(a, b);
        }
    }
}

Upvotes: -1

Chris Cooper
Chris Cooper

Reputation: 5122

It looks like you are generating the nth number in the Fibonacci sequence, it seems to me, you need to store all the values you are generating (not just the last one) and display them.

So rather than just f(n), you need to display f(1), f(2), ..., f(n-1), f(n) once you have done that you have completed your assignment.

Upvotes: 2

Related Questions