fibonacci problem using while loop

This is a discussion on fibonacci problem using while loop within the C Programming forums, part of the General Programming Boards category; hi i have been working hard on a c program of how to print the first 10 fibonacci numbers? using ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    fibonacci problem using while loop

    hi
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    a little google couldn't hurt:
    http://alphasystem.dnsalias.net/Docs.../Examples/C16/

    fibonacci.h and fibonacci.cpp
    It is C++ but since you're interesting in loops it could be helpful
    Or maybe this:
    http://cubbi.org/serious/fibonacci/c.html

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Code:
    int main(){
    	int f0=0, f1=1, f2=1, i=0;
    
    	while(i<10){
    		/*print the number, and calculate the next one*/
    		i++;
    	}
    	return 0;
    }
    Very simple

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    Micko,
    The recursion version is pretty sweet.
    cubbi.org 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
    Posts
    4
    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
    Posts
    30

    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

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
        int result=1,term1=0,term2=1,i=1;
        printf("%d ",result);
        while(i<10)
        {
            result = term1 + term2;
            printf("%d ",result);
            term1 = term2;
            term2 = result;
            i++;
        }
        getch();
        return 0;
    }
    Syra
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21