The Fibonacci sequence is named after Italian mathematician Leonardo of Pisa, known as Fibonacci. His 1202 book Liber Abaci introduced the sequence to Western European mathematics, although the sequence had been described earlier as Virahanka numbers in Indian mathematics. By modern convention, the sequence begins either with F0 = 0 or with F1 = 1.
The next number is found by adding up the two numbers before it.
The 2 is found by adding the two numbers before it (1 + 1), similarly, the 3 is found by adding the two numbers before it (1 + 2), next the 5 is (2+3), and so on.
Example: the next number in the sequence above is 21 + 34 = 55
It is that simple, and here is a longer list:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, ...
Here is the source code.
/*
Program untuk menghitung nilai fibonacci suatu bilangan
Loki Lang
*/
#include <iostream>
using namespace std;
int fib (int i)
{
int pred, result, temp;
pred = 1;
result = 0;
while (i > 0)
{
temp = pred + result;
result = pred;
pred = temp;
i = i-1;
}
return(result);
}
int main()
{
int n;
cout<<"Masukkan sebuah bilangan bulat positif: ";
cin>>n;
while(n < 0)
{
cout<<"Kesalahan bilangan negatif"<<endl;
cout<<"Silahkan masukkan kembali sebuah bilangan bulat positif: ";
cin>>n;
}
cout<<"Nilai Fibbonacci bilangan (" <<n<< ") adalah "<<fib(n)<<endl;
return(0);
}
Program untuk menghitung nilai fibonacci suatu bilangan
Loki Lang
*/
#include <iostream>
using namespace std;
int fib (int i)
{
int pred, result, temp;
pred = 1;
result = 0;
while (i > 0)
{
temp = pred + result;
result = pred;
pred = temp;
i = i-1;
}
return(result);
}
int main()
{
int n;
cout<<"Masukkan sebuah bilangan bulat positif: ";
cin>>n;
while(n < 0)
{
cout<<"Kesalahan bilangan negatif"<<endl;
cout<<"Silahkan masukkan kembali sebuah bilangan bulat positif: ";
cin>>n;
}
cout<<"Nilai Fibbonacci bilangan (" <<n<< ") adalah "<<fib(n)<<endl;
return(0);
}