📘 Lesson  ·  Lesson 74

Fibonacci Series

Fibonacci Series

Fibonacci Series in Python (Loop and Recursion)

The Fibonacci series is a sequence where each number is the sum of the two before it (0, 1, 1, 2, 3, 5...).

Python Program

Python
# Using a loop
def fibonacci(n):
    a, b = 0, 1
    for _ in range(n):
        print(a, end=" ")
        a, b = b, a + b

fibonacci(8)

# Using recursion
def fib(n):
    if n <= 1:
        return n
    return fib(n-1) + fib(n-2)
print("\n5th term:", fib(5))

Expected Output

0 1 1 2 3 5 8 13 5th term: 5

How it Works

  • Loop version: keep two numbers and swap them to get the next.
  • Recursion: fib(n) = fib(n-1) + fib(n-2), with base case n <= 1.

Summary

  • The Fibonacci series is a sequence where each number is the sum of the two before it (0, 1, 1, 2, 3, 5...).

Fibonacci Series in Python (Loop and Recursion)

Fibonacci series एक अनुक्रम है जहाँ हर संख्या अपने से पहले की दो संख्याओं का योग होती है (0, 1, 1, 2, 3, 5...)।

Python Program

Python
# Using a loop
def fibonacci(n):
    a, b = 0, 1
    for _ in range(n):
        print(a, end=" ")
        a, b = b, a + b

fibonacci(8)

# Using recursion
def fib(n):
    if n <= 1:
        return n
    return fib(n-1) + fib(n-2)
print("\n5th term:", fib(5))

Expected Output

0 1 1 2 3 5 8 13 5th term: 5

कैसे काम करता है

  • Loop version: दो संख्याएं रखें और swap करके अगली पाएं।
  • Recursion: fib(n) = fib(n-1) + fib(n-2), base case n <= 1।

सारांश

  • Fibonacci series एक अनुक्रम है जहाँ हर संख्या अपने से पहले की दो संख्याओं का योग होती है (0, 1, 1, 2, 3, 5...)।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n