Can anyone help me please with a fibonacci program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayman723
    New Member
    • Sep 2006
    • 40

    Can anyone help me please with a fibonacci program?

    I wrote this program code :

    # include < iostream >

    # include < iomanip >

    using namespace std;

    unsigned long factorial ( unsigned long );

    int main ()

    {

    for ( int i = 0 ; i <= 10; i ++ )

    cout << setw ( 2) << i << " ! = " << factorial (i) << endl;

    return 0;

    }

    unsigned long factorial ( unsigned long number )

    {

    unsigned long product = 1;

    for ( unsigned long i=1; i<=number; ++i )

    {

    product = product * i ;

    }

    return product;

    }

    How can I determine the largest fibonacci number that can be printed on my system ?
  • ayman723
    New Member
    • Sep 2006
    • 40

    #2
    anyone please.

    Comment

    • vermarajeev
      New Member
      • Aug 2006
      • 180

      #3
      Originally posted by ayman723
      anyone please.
      Hi..
      This is like spoon feeding...Pleas e try something rather than depending on others. This is a simple program and please try to give an attempt... Also please dont send the same query again and again. It irritates a lot.

      This query I have seen for almost 3 times....

      Try out yourself....I have already given a solution for your question...Try understanding yourself. Also please dont ask any stupid question again.

      Thanx

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by ayman723
        anyone please.
        First find the largest number that can be printed on your system.
        Then the problem becomes that of finding the largest fibbo number less or equal to a number

        Comment

        • ayman723
          New Member
          • Sep 2006
          • 40

          #5
          Originally posted by r035198x
          First find the largest number that can be printed on your system.
          Then the problem becomes that of finding the largest fibbo number less or equal to a number

          THANKS FOR YOUR KIND ANSWER , I'LL TRY IT AND LET YOU KNOW

          THANKS AGAIN
          AYMAN

          Comment

          • ayman723
            New Member
            • Sep 2006
            • 40

            #6
            Originally posted by ayman723
            THANKS FOR YOUR KIND ANSWER , I'LL TRY IT AND LET YOU KNOW

            THANKS AGAIN
            AYMAN

            ---------


            I GOT 13! WAS THE LAST CORRECT NUMBER, 14! LOOKED FUNNY. DOES THAT MEANS THAT 13! WAS THE HIGHEST NUMBER ?

            Comment

            • ayman723
              New Member
              • Sep 2006
              • 40

              #7
              i also want to add that eventhough 14! is giving me funny numbers, but I was gitting results ( funny results ) up to 33!, so which one I should considir highest 14! or 33! ?

              thanks
              ayman

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by ayman723
                ---------


                I GOT 13! WAS THE LAST CORRECT NUMBER, 14! LOOKED FUNNY. DOES THAT MEANS THAT 13! WAS THE HIGHEST NUMBER ?
                I am not sure if you are aware of this but in normal net etiquet using all capitals is considered to be shouting and some what aggresive or rude.

                Comment

                • D_C
                  Contributor
                  • Jun 2006
                  • 293

                  #9
                  Fibonacci or Factorial?

                  Fib(n) = Fib(n-1) + Fib(n-2)
                  Fact(n) = n*Fact(n-1)

                  When the number gets too large, it will overflow. That means the sign changes. This is always true for addition. In general, it's not always the case for multiplying two numbers. However, for factorials it is.

                  Write a loop that calculates the next factorial or Fibonacci number. After calculating it, compare it to the previous one. If the previous one is now larger, then it overflowed. The previous of the two is the biggest your system can handle.

                  Comment

                  • vermarajeev
                    New Member
                    • Aug 2006
                    • 180

                    #10
                    How can I determine the largest fibonacci number that can be printed on my system ?
                    Hi check this...Is this what you want???
                    I' ve not checked the output...Please test this program extensively and let me know if it works for all machines.....

                    Code:
                    long fact(long num)
                    {		
                      long product = 1;	
                      for(long i=1; i<=num; ++i)
                      {
                         try
                         {
                            product *= i;
                            if((INT_MAX - product) < product*(i+1))
                            {
                    	throw "Reached Max limit";				 
                            }				
                         }
                         catch (char* msg)
                         {	
                             cout<<msg<<endl;				 
                            cout<<"The max factorial value your system can       handle :"<<product<<endl;
                            return -1;
                          }
                       }
                        return product;
                    }
                    
                    int main(int argc, char** argv[])
                    {
                         for ( long i = 0 ; i <= 15; i++ )
                          {
                             int val = fact(i);
                             if( val == -1 )
                    	break;
                             else
                    	cout << setw ( 2) << i << " ! = " << val << endl;
                           }	    				
                          return 0;
                    }

                    Comment

                    • ayman723
                      New Member
                      • Sep 2006
                      • 40

                      #11
                      Originally posted by Banfa
                      I am not sure if you are aware of this but in normal net etiquet using all capitals is considered to be shouting and some what aggresive or rude.

                      -------------

                      dear Banfa

                      I didn't know that, but thank you for letting me know.

                      Ayman723

                      Comment

                      • vermarajeev
                        New Member
                        • Aug 2006
                        • 180

                        #12
                        Originally posted by ayman723
                        -------------

                        dear Banfa

                        I didn't know that, but thank you for letting me know.

                        Ayman723
                        Hi man!! did that help u????????

                        Comment

                        • vermarajeev
                          New Member
                          • Aug 2006
                          • 180

                          #13
                          Originally posted by ayman723
                          -------------

                          Ayman723
                          Hi man!! did that help u????????

                          Comment

                          • ayman723
                            New Member
                            • Sep 2006
                            • 40

                            #14
                            Originally posted by vermarajeev
                            Hi man!! did that help u????????
                            thanks man. that helped me finish my project. I couldn't have done without your help guys.

                            thanks a million
                            ayman723

                            Comment

                            Working...