Program describes an easy way to find the Fibonacci numbers below a given number.
Logic: Fibonacci numbers are the the numbers fall on the Fibonacci series, which grows up accord to Fibonacci rule. The rule is that, the present number in the series is the sum of past number and the number before last. The series starts from 0, and the 2nd term is 1.
Mathematically,
F(n+1) = F(n) + F(n-1) // F(1) =0 and F(2) = 1
Programmatically, that is what exactly we are doing. The while loop traces till the upper limit. In each iteration we keep a flag to store last to iteration results to proceed to the next.
To find the fibonacci numbers below a given number.
#include<stdio.h>
void main()
{
int lim_up, A = 0, B = 1, C;
clrscr();
printf(“\n\n\t ENTER THE UPPER LIMIT…: “);
scanf(“%d”, &lim_up);
printf(“\n\n\t FIBONACCI NUMBERS ARE…: “);
while(A<lim_up)
{
printf(“\n\n\t\t\t%d”, A);
C = A + B;
A = B;
B = C;
}
getch();
}
Download exe and source code here.
The algebraic proof does porvide certain insights as well. For example, it tells you that the property,F(n+1)^2 – F(n)*F(n+2) = –[F(n)^2 – F(n–1)*F(n+1)] for all n,is independent of the initial values F(1) = 1, F(2) = 1, and therefore that something like Cassini’s identity will hold for any choice of initial conditions.This property can be rewritten F(n+1)^2 + F(n)^2 = F(n)*F(n+2) + F(n–1)*F(n+1), which has a simple geometric interpretation:The area of the two squares* * * * * * * ** * * * * * * ** * * * * * * ** * * * ** * * * *is the same as that of the two rectangles* * * * * * * ** * * * * * * ** * * * * * * ** * * * ** * * * *