fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // Barisan fibonacci 0 1 1 2 3 5 8 13 21 34 55 89 ...
  6. int n, fibo1 = 0, fibo2 = 1;
  7. cin >> n;
  8. if(n == 1)
  9. cout << "0";
  10. else {
  11. cout << "0 1 ";
  12. for(int i = 3; i <= n; i++) {
  13. int jumlah = fibo1+fibo2;
  14. cout << jumlah << " ";
  15. fibo1 = fibo2; // 3 -> 5 -> 8
  16. fibo2 = jumlah; // 5 -> 8 -> 13
  17. }
  18. }
  19. cout << endl;
  20. return 0;
  21. }
Success #stdin #stdout 0.01s 5292KB
stdin
10
stdout
0 1 1 2 3 5 8 13 21 34