Thread: fibonacci problem using while loop

  1. #1
    Registered User
    Join Date
    Sep 2004

    fibonacci problem using while loop

    i have been working hard on a c program of how to print the first 10 fibonacci numbers?
    using while loop....
    but i have come out of nothing.... but i know the concept of fibonacci numbers though
    but still...i am confused that do i have to declare 10 variables if
    after declaring suppose variable 1 as 0 and varaible 2 as 1 then variable 3=varable 1 +variable 2 and i'll be swapping the sum of previous two terms to get a next term as it happens in the series of fibonacci numbers.
    plz write a code of this program and help me make me understand using while loop.....
    thx ....
    waiting for ur reply guys!!!!!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    first = 0;
    second = 1;
    next = first + second;

    Having done that, you shuffle the series
    first = second;
    second = next;

    Now put that lot in a while loop and you're done
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    a little google couldn't hurt:

    fibonacci.h and fibonacci.cpp
    It is C++ but since you're interesting in loops it could be helpful
    Or maybe this:

  4. #4
    Registered User
    Join Date
    Jun 2004
    int main(){
    	int f0=0, f1=1, f2=1, i=0;
    		/*print the number, and calculate the next one*/
    	return 0;
    Very simple

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    The recursion version is pretty sweet. is a nice website.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  6. #6
    Deepcore Programmer
    Join Date
    Sep 2004
    Recursion version may be sweet, but it's a proved fact that a recursive algorithm of finding fibonacci numbers is WAY slower than an algorithm that is non-recursive (i.e. uses arrays to store the growing fibonacci numbers). Because in the first case, each time a new fibonacci number is calculated, all previous numbers are calculated all over and over again. It may be sweet to read, but for the processor, it's a painful algorithm to work through.

    Just thought I'd mention it, in case you don't want to spend minutes finding the 47th or 48th Fibonacci number

  7. #7
    Registered User
    Join Date
    Oct 2004

    try it

    the solution is simple

    keep track of two variables term1 & term2
    add them and store the resulting value in result
    update the variables

    int main(void)
        int result=1,term1=0,term2=1,i=1;
        printf("%d ",result);
            result = term1 + term2;
            printf("%d ",result);
            term1 = term2;
            term2 = result;
        return 0;
    Amateur's urge to master C/C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 06:14 AM