Advertisement
amu2002

Fibonacii

Nov 20th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. print("\nFibonacci using Recursion")
  2. n = int(input("Enter number of terms:"))
  3. def fibonacci(n):
  4.     if(n <= 1):
  5.         return n
  6.     else:
  7.         return(fibonacci(n-1) + fibonacci(n-2))
  8. print("\nFibonacci using Non-Recursion")
  9. print("Fibonacci sequence:")
  10. for i in range(n):
  11.     print(fibonacci(i))
  12.  
  13. a = int(input("Enter the first number of the series: "))
  14. b = int(input("Enter the second number of the series: "))
  15. num = int(input("Enter the number of terms needed: "))
  16. print("Fibonacci sequence: ")
  17. print(a, b, end=" ")
  18.  
  19. while (num - 2):
  20.     c = a + b
  21.     a = b
  22.     b = c
  23.     print(c, end=" ")
  24.     num = num - 1
  25.  
  26.  
  27. """
  28. Fibonacci using Recursion
  29. Enter number of terms:4
  30.  
  31. Fibonacci using Non-Recursion
  32. Fibonacci sequence:
  33. 0
  34. 1
  35. 1
  36. 2
  37. Enter the first number of the series: 0
  38. Enter the second number of the series: 1
  39. Enter the number of terms needed: 10
  40. Fibonacci sequence:
  41. 0 1 1 2 3 5 8 13 21 34
  42. """
Tags: daa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement