Fibonacci Series

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kookai
    New Member
    • Jul 2006
    • 4

    Fibonacci Series

    hi
    I need help with this problem. I have tried writting the program, i just can not get it to run properly.

    Please Write
    The Fibonacci series
    0, 1, 1, 2, 3, 5, 8, 13, 21, ...
    begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
    (a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.

    and than you
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    This is very simply achieved with a loop, you should have a go yourself and then we will help if there are problems in your solution.

    There is a formula as well which you should be able to find with a few minutes web search. However it uses the floating point calculations on an irrational number so may not be entirely suited to a computer (where floating point calculations are not exact).

    Comment

    • D_C
      Contributor
      • Jun 2006
      • 293

      #3
      There is a simple recurrence relation which will help.

      fibonacci(n) = fibonacci(n-1) + fibonacci(n-2). That's the recurrence relation which is recursive, but it is easily implemented as a loop.

      Comment

      • shinelakshmanan
        New Member
        • Aug 2006
        • 13

        #4
        //Non recursive method
        Code:
        #include<stdio.h>
        #include<conio.h>
        	
        void main()
        {
           int initial_value=0,final_value=1,temp_value,count=10;
        // count contains the number of elements to be generated.....
         for(count=1;count<=10;count++)
          {
                        temp_value=initial_value+final_value;
        	printf("\n%d",initial_value);
        	initial_value=final_value;
        	final_value=temp_value;
        }
        
        getch();
        }
        Last edited by Frinavale; Sep 28 '10, 02:11 PM. Reason: Please post code in code tags.

        Comment

        • shinelakshmanan
          New Member
          • Aug 2006
          • 13

          #5
          Code:
          // Recursive Method.......
          
          int fibo_gen(int,int,int);
          
          void main()
          
          {
          
            int initial_value=0,final_value=1,count=100;
            fibo_gen(initial_value,final_value,count);
            getch();
          
          }
          
          int fibo_gen(int initial_value,int final_value,int count)
          
          {
          
           int temp;
          
          if (count>0)
          
           {
          
            printf("\n%d",initial_value);
            temp=initial_value+final_value;
            initial_value=final_value;
            final_value=temp;
            count=count-1;
            fibo_gen(a,b,c);
          	
          }
          
          else
          return(0);
          
          }
          ]
          Last edited by Frinavale; Sep 28 '10, 02:12 PM. Reason: Please post code in code tags.

          Comment

          • gondarala vaibhavi
            New Member
            • Jul 2006
            • 1

            #6
            Code:
            main()
            {
            int n,fib[20];
            printf("enter the value of n");
            scanf("%d",&n);fib[0]=0;fib[1]=1;
            for(i=2;i<n;i++)
            fib(i)=fib(i-1)+fib(i-2);
            Last edited by Frinavale; Sep 28 '10, 02:12 PM. Reason: please post code in code tags.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by gondarala vaibhavi
              main()
              {
              int n,fib[20];
              printf("enter the value of n");
              scanf("%d",&n); fib[0]=0;fib[1]=1;
              for(i=2;i<n;i++ )
              fib(i)=fib(i-1)+fib(i-2);
              You haven't output the result,
              you haven't finished the program, this wont compile
              if the user inputs a value >= 20 then this program writes off the end of the fib array invoking undefined behaviour.

              Apart from that a very good solution :D

              Comment

              • kookai
                New Member
                • Jul 2006
                • 4

                #8
                G'day

                where error?

                1 error(s), 0 warning(s)



                Code:
                # include <iostream.h>
                int fibonacci(int n)
                {
                int x1 = 0, fib;
                int x2 = 1;
                if(n >= 1)
                {
                for(int i=2;i<= n; i++)
                {
                fib = x1+ x2;
                x1 = x2;
                x2 = fib;
                }
                
                return fib;
                }
                Last edited by Frinavale; Sep 28 '10, 02:12 PM. Reason: Please post code in code tags.

                Comment

                • D_C
                  Contributor
                  • Jun 2006
                  • 293

                  #9
                  You only return a value if n > 0. Afterwards, you should add "return 1; }" assuming n is non-negative. Also, usually the sequence starts with 1 and 1, not 0 and 1. You may be off by one term.

                  Comment

                  • kookai
                    New Member
                    • Jul 2006
                    • 4

                    #10
                    #include<iostre am>
                    using namespace std;
                    Code:
                    int fibonacci(int n)
                    {
                      int x1 = 0, fib;
                      int x2 = 1;
                      if(n >= 1)
                      {
                        for(int i=2;i<= n; i++)
                        {
                          fib = x1+ x2;
                          x1 = x2;
                          x2 = fib;
                        }
                      }
                    
                      return fib;
                    }
                    int main(){
                      for(int i=2;i<=10;i++) {
                        cout<<fibonacci(i)<<endl;
                      }
                      return 0;
                    }
                    this is ok now I'm finish it
                    but I need this


                    (1) Determine the largest int Fibonacci number that can be printed on your system.
                    (2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).
                    Last edited by Frinavale; Sep 28 '10, 02:13 PM. Reason: Please post code in code tags.

                    Comment

                    • D_C
                      Contributor
                      • Jun 2006
                      • 293

                      #11
                      The way integers are ordered, are non-negative numbers, in order, followed by the reversed negative range. It goes from 0 to INT_MAX, then INT_MIN to -1. Integers are 32-bit, so in Hexadecimal:
                      0x00000000 = 0
                      0x7FFFFFF = INT_MAX = -1+(2^31)
                      0x80000000 = INT_MIN = -(2^31)
                      0xFFFFFFF = -1.

                      INT_MAX + 1 = INT_MIN. Since Fibonacci numbers are positive, it will overflow and become negative. Modify the for statement to a while statement, and run the loop while fib is not negative (no overflow). Then variables x1 and x2, x1 < x2, will be the two most positive Fibonacci numbers an integer can support.

                      Comment

                      • suresh_punniyakkodi
                        New Member
                        • Aug 2006
                        • 20

                        #12
                        Hi,
                        Please follow the following program,

                        Code:
                        #include<stdio.h>
                        #include<conio.h>
                        void main()
                        {
                          int a =0,b=1,c,i=2,n;
                          clrscr();
                          printf("Enter N Value : ");
                          scanf("%d",&n);
                          printf("%d\t%d\t",a,b);
                          while(n>i)
                          {
                            c = a+b;
                            printf("%d\t",c);
                            c=b;
                            b=a;
                            i++;
                          }
                          getch();
                        }
                        Take Care,
                        Bye... Bye...
                        Last edited by Frinavale; Sep 28 '10, 02:14 PM. Reason: Please post code in code tags.

                        Comment

                        • DrXavier
                          New Member
                          • Aug 2006
                          • 3

                          #13
                          The easiset non recursive method I see to do this is to use a stack.

                          stack will always have 2 elements , both will have integers, and of course pointers.

                          The next element will be figured by adding the 2 integers in the stack. then you will push that on the top of the stack, and pull the header off of the stack.

                          I hope that made sence.

                          Comment

                          • sach06jan
                            New Member
                            • Sep 2006
                            • 2

                            #14
                            Originally posted by kookai
                            hi
                            I need help with this problem. I have tried writting the program, i just can not get it to run properly.

                            Please Write
                            The Fibonacci series
                            0, 1, 1, 2, 3, 5, 8, 13, 21, ...
                            begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
                            (a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.

                            and than you
                            hello friend,
                            one thing is remember whenever u r doing fib series most important part is fib generate recursion that is chain is occur. so make one function is fib(). around this function write the code just like this as follows
                            fo=0;
                            f1=1
                            fib=fo+f1;
                            and display the fib.
                            try your self otherwise i will send u full program for fib.
                            thank u very much.

                            Comment

                            • D_C
                              Contributor
                              • Jun 2006
                              • 293

                              #15
                              Instead of a stack with two entries, just use two variables. Besides, there are already plenty of solutions posted here, and the iterative loop is considerably faster than using recursion.

                              Comment

                              Working...