This discussion is archived
1 2 Previous Next 22 Replies Latest reply: Apr 19, 2013 5:01 PM by 967983 Go to original post RSS
  • 15. Re: Fibonnaci Recursion without for or while loop
    967983 Newbie
    Currently Being Moderated
    I am trying to print recursively without using for or while loop,but there is some flaw in the program. Any guidance in the right direction is appreciated
    public class Sample1 {
         static int[] fib=new int[10000];
         int a;
         int flag0=0;
         int flag1=0;
         public static void main(String[] args) {
              Sample1 st = new Sample1();
              fib[0]=0;
              fib[1]=1;
              st.fib(5);
     
         }
     
     
         int fib(int n)
         {
              if(n==0)
              {
                   flag0=1;
                   return 0;
              }
              if(n==1)
              {                              
                   flag1=1;
                   return 1;
              }
              a=(fib(n-1) + fib(n-2));
              print(a);       
              return a;
         }
         int print(int v)
         {
              if((v==0)||(v==1))
              {
                   System.out.print(v+",");               
              }
              else
              {
                   v=v+print(v-1);               
              }
              return v;
         }
    }
  • 16. Re: Fibonnaci Recursion without for or while loop
    967983 Newbie
    Currently Being Moderated
    In the else part of the print function it makes more sense to have, I think

    v=print(v-1);
  • 17. Re: Fibonnaci Recursion without for or while loop
    EJP Guru
    Currently Being Moderated
    You people, being such senior members of the forum should not talk like this. You must always say it's possible.
    It is just sheer driveling nonsense to assert that people must say things are possible even when they aren't.

    If you think this is possible, the onus is on you to prove it. You can't. The purpose of the recursive function is to evaluate Fibonacci numbers, not to print them, and by its nature it is called somewhat more often than required to print them.

    I suggest you try using this code to evaluate the 60th Fibonacci number. Then you might get the idea (1) that this is not the most efficient way to compute them, and (2) that your objective of printing them inside the recursion is, to put it mildly, somewhat misplaced.
  • 18. Re: Fibonnaci Recursion without for or while loop
    967983 Newbie
    Currently Being Moderated
    If suppose I store the values in an array as one of the members suggested, can I later print the values of the array by calling a function recursively without using for or while loop?

    Is it possible?
  • 19. Re: Fibonnaci Recursion without for or while loop
    967983 Newbie
    Currently Being Moderated
    I was just trying print values of an array without using for loop and just by using recursion. Not a very good approach, I must say because I am using System.exit(0) in the code but still it is an option if your requirement is to print array values recursively, without using a for loop.

    If there is a better option than using System.exit(0), please let me know.
    public class Try {
         static int count=0;
         static int[] i1={1,2,3,4,5};
         public static void main(String args[])
         {
              
              print(i1[0]);
              
         }
         static void print(int val)
         {
              
              if(count==i1.length-1)
              {
                   System.out.println(i1[count]);
                   System.exit(0);
              }
              else
              {
                   System.out.println(i1[count]);
                   count++;
                   print(i1[count]);
              }
              
         }
    }
  • 20. Re: Fibonnaci Recursion without for or while loop
    800268 Expert
    Currently Being Moderated
    964980 wrote:
    If there is a better option than using System.exit(0), please let me know.
    What happens when you just leave it out?

    Also why do you have a 'val' parameter?
    How would you write the function if count and i1 were defined in the main method (so not as static class fields)?
  • 21. Re: Fibonnaci Recursion without for or while loop
    967983 Newbie
    Currently Being Moderated
    What happens when you just leave it out?
    Yes System.exit(0) is unrequired. My mistake.

    Also why do you have a 'val' parameter?
    Again my mistake.

    How would you write the function if count and i1 were defined in the main method (so not as static class fields)?
    I tried to do it, but I am getting compile time error, saying count cannot be resolved to a field or variable. The code I tried is given below. Thanks for your time and sorry for the stupid questions, but I am eager to know the answer. Also, I am not from CS background. Kindly, bear with me.
    public class Try {
         public static void main(String args[])
         {
              int count=0;
              int[] i1={1,2,3,4,5};
              
              print();
              
         }
         static void print()
         {
              System.out.println(Try.main(null).count);
         }
    }
    Edited by: 964980 on Apr 19, 2013 4:44 PM
  • 22. Re: Fibonnaci Recursion without for or while loop
    967983 Newbie
    Currently Being Moderated
    Yipee, found the solution. By passing the parameters as arguments to the print function we don't have to declare it as class level variables. If there is any other way of accessing the variable defined in main function, let me know.
    public class Try {
         public static void main(String args[])
         {
              int count=0;
              int[] i1={1,2,3,4,5};
              
              print(count,i1);
              
         }
         static void print(int a, int[] b)
         {
              int len=b.length-1;
              if(a==len)
              {
                   System.out.println(b[a]);
              }
              else
              {
                   System.out.println(b[a]);
                   a++;
                   print(a,b);
              }
         }
    }
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points