![]() |
Fibonacci series in C and Cplus Cplus |
In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting
from 0 and 1.
like 0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
3 + 2 = 5
5 + 3 = 8
8 + 5 = 13
13 + 8 = 21
21 + 13 = 34
34 + 21 = 55
55 + 34 = 89
89 + 55 = 144
144 + 89 = 233 ........... infinite
The result will be add with previous number and in the same way the series moves forward.
GCD IN C / C++
]
Click Here :- GCD
Logic
int first = 0 , second = 1 , next; // next means result
next = first + second;
first = second; // second value transfer into first
seond = next;
Output
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 and so on.....
Q) wap in c to find a Fibonacci number.
#include<iostream.h>
using namespace std;
main()
{
int n, c, first = 0, second = 1, next;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
return 0;
}
Result
Enter the number of terms of Fibonacci series you want
First 10 terms of Fibonacci series are :-
0
1
1
2
3
5
8
13
21
34
Have a Nice Stay Here :- )
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.