0

Hi Everyone!
I'm a user of Turbo C++ and my professor gave me an activity to make a Fibonacci sequence.
No I already have the Code for it and it is working. The only problem is. I don't know how the FOrmula Works.
I will show you the Code below:

include<stdio.h>

include<conio.h>

int x, num, f0 = 0, f1 = 1, fibo;
main()
{
    clrscr();
    printf("Enter a number: ");
    scanf("%i",&num);

    for (x=0;x<num;x++) {
        //fibo means fibonacci. 
        fibo = f0+f1, f0=f1, f1=fibo;

        printf(" %i",fibo);
        }

    getch();
}

IF you can just tell me how this FOrmula works: fibo = f0+f1, f0=f1, f1=fibo; That would be really great.
You guys can just reply to this thread or Email me the explanation: [nope]

Thank you! :D

Edited by WaltP: Don't post email addresses

4
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by vinnitro
1

Well do you know what the formula for the fibonacci squence is? If not it is Fn = Fn-1 +Fn-2 where F0=0 and F1=1. so to get a fib number you have to take the previous number plus the number before the previous number. Can you see know how the code is working?

Votes + Comments
Thanks for the formula! Haha
1

Hey try this one

#include<stdio.h>
int main(){
    int k,r;
    long int i=0l,j=1,f;



//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);

printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.

for(k=2;k<r;k++){
     f=i+j;
     i=j;
     j=f;
     printf(" %ld",j);
}

return 0;


}

Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

0

We assume first two Fibonacci are 0 and 1
A series of numbers in which each sequent number is sum of its two previous numbers is known as Fibonacci series and each numbers are called Fibonacci numbers. So Fibonacci numbers is

Algorithm for Fibonacci series

Fn = Fn-2 + Fn-1

Example of Fibonacci series:

0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ...

5 is Fibonacci number since sum of its two previous number i.e. 2 and 3 is 5
8 is Fibonacci number since sum of its two previous number i.e. 3 and 5 is 8 and so on.

2

At first f0 = 0, f1 = 1 & fibo has no value.
x is used for working of for loop & num is for number of repeatations of loop.

eg. You entered num = 5 then for loop start executing
in the code you haven't printed first 2 numbers
0 & 1
for loop when
originally f0 = 0 & f1 = 1.
x = 1 & num = 5 i.e. x < 5 = true. fibo = f0 + f1. fibo = 0 + 1 i.e fibo = 1. f0 = 1 (f0 = f1) & f1 = 1 (f1 = fibo).
x = 2 & num = 5 i.e. x < 5 = true. fibo = f0 + f1. fibo = 1 + 1 i.e fibo = 2. f0 = 1 (f0 = f1) & f1 = 2 (f1 = fibo).
x = 3 & num = 5 i.e. x < 5 = true. fibo = f0 + f1. fibo = 1 + 2 i.e fibo = 3. f0 = 2 (f0 = f1) & f1 = 3 (f1 = fibo).
x = 4 & num = 5 i.e. x < 5 = true. fibo = f0 + f1. fibo = 2 + 3 i.e fibo = 5. f0 = 3 (f0 = f1) & f1 = 5 (f1 = fibo).
x = 5 & num = 5 i.e. x < 5 = false.
Loop will terminate now

So your program will output like this with input num = 5:
1 2 3 5
since you forgot to print 0 & 1

Votes + Comments
Thanks! This is the one that I understand
0

Hey Guys!
Thank you all for your replies.
Now I Understand how the Fibonacci Sequence and formula works.
Thank You! Especially @vinnitro.

Edited by Grandiago: Wrong spelling. lol

0

I always tries to trace the program to check how it works so it becomes easy to understand & difficult for me to forget.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.