main.cpp 540 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <iostream>
  2. using namespace std;
  3. bool Fibon_Elem(int pos, int& elem) {
  4. if (pos >= 1024 || pos <= 0) {
  5. elem = 0;
  6. return false;
  7. }
  8. elem = 1;
  9. int first = 1, second = 1;
  10. for (int ix = 3; ix <= pos; ++ix) {
  11. elem = first + second;
  12. first = second;
  13. second = elem;
  14. }
  15. return true;
  16. }
  17. int main() {
  18. int elem = 0;
  19. bool ret = false;
  20. for (int i = 0; i < 10; ++i) {
  21. ret = Fibon_Elem(i, elem);
  22. if (!ret)
  23. cout << "Position " << i <<" is invalid!" << endl;
  24. else
  25. cout << "ret=" << ret << " " << elem << endl;
  26. }
  27. }