main.cpp 531 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <iostream>
  2. using namespace std;
  3. bool Fibon_Elem(int pos, int& elem) {
  4. bool ret = false;
  5. elem = 1;
  6. int first = 1, second = 1;
  7. if (pos <= 0) {
  8. return ret;
  9. }
  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. }