Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 27,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "Enter number of terms: 4\n",
  13. "The Fibonacci sequence consisting of 4 elements is:\n",
  14. "0\n",
  15. "1\n",
  16. "1\n",
  17. "2\n"
  18. ]
  19. }
  20. ],
  21. "source": [
  22. "n = int(input(\"Enter number of terms: \"))\n",
  23. "def fib(n):\n",
  24. " if(n <= 1):\n",
  25. " return n\n",
  26. " else:\n",
  27. " return(fib(n-1) + fib(n-2))\n",
  28. "\n",
  29. "print(\"The Fibonacci sequence consisting of\",n ,\"elements is:\")\n",
  30. "for i in range(n):\n",
  31. " print (fib(i))"
  32. ]
  33. },
  34. {
  35. "cell_type": "code",
  36. "execution_count": null,
  37. "metadata": {},
  38. "outputs": [],
  39. "source": [
  40. "#Alternative Approach (by using while loop)"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": 28,
  46. "metadata": {},
  47. "outputs": [
  48. {
  49. "name": "stdout",
  50. "output_type": "stream",
  51. "text": [
  52. "Enter the number of elements of the series 6\n",
  53. "Fibonacci series including 6 elements is:\n",
  54. "0\n",
  55. "1\n",
  56. "1\n",
  57. "2\n",
  58. "3\n",
  59. "5\n"
  60. ]
  61. }
  62. ],
  63. "source": [
  64. "x=int(input(\"Enter the number of elements of the series \"))\n",
  65. "a=0\n",
  66. "b=1\n",
  67. "i=2\n",
  68. "if x<= 0:\n",
  69. "\tprint (\"Enter a positive integer: \")\n",
  70. "elif x== 1:\n",
  71. "\tprint (\"Fibonacci series including one element is\",x,\":\")\n",
  72. "\tprint (a)\n",
  73. "else:\n",
  74. "\tprint (\"Fibonacci series including\",x,\"elements is:\")\n",
  75. "\tprint (a)\n",
  76. "\tprint (b)\n",
  77. "\t\n",
  78. "\twhile i<x:\n",
  79. "\t\tc=a+b\n",
  80. "\t\tprint (c)\n",
  81. "\t\ta=b\n",
  82. "\t\tb=c\n",
  83. "\t\ti=i+1"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": null,
  89. "metadata": {},
  90. "outputs": [],
  91. "source": []
  92. },
  93. {
  94. "cell_type": "code",
  95. "execution_count": null,
  96. "metadata": {},
  97. "outputs": [],
  98. "source": []
  99. }
  100. ],
  101. "metadata": {
  102. "kernelspec": {
  103. "display_name": "Python 3",
  104. "language": "python",
  105. "name": "python3"
  106. },
  107. "language_info": {
  108. "codemirror_mode": {
  109. "name": "ipython",
  110. "version": 3
  111. },
  112. "file_extension": ".py",
  113. "mimetype": "text/x-python",
  114. "name": "python",
  115. "nbconvert_exporter": "python",
  116. "pygments_lexer": "ipython3",
  117. "version": "3.7.3"
  118. }
  119. },
  120. "nbformat": 4,
  121. "nbformat_minor": 2
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement