Programme To Print Fibonacci Seiers Upto User Specified Number of Terms Which Wil Be Inputted by The User of A Number Display The Result Write The Programme in The Form of Logic
Programme To Print Fibonacci Seiers Upto User Specified Number of Terms Which Wil Be Inputted by The User of A Number Display The Result Write The Programme in The Form of Logic
different approaches. The Fibonacci sequence starts with 0 and 1, and each
subsequent term is the sum of the previous two terms.
---
To print the Fibonacci series without any loops or conditional statements, we can
use a recursive approach. However, recursion inherently has its limits for larger
terms in the Fibonacci sequence, so this solution assumes a small, fixed number of
terms for illustration.
#### **Logic**
1. Define a recursive function to calculate Fibonacci terms.
2. Call the function for each term manually.
#### **Algorithm**
1. Start
2. Define a recursive function `fibonacci(n)` that returns the `n`-th Fibonacci
term.
3. Input the number of terms, `n`.
4. Manually call the `fibonacci` function up to `n` times and print each term.
5. End
```python
# Step 1: Define a recursive function to calculate Fibonacci terms
def fibonacci(n):
if n == 1:
return 0
elif n == 2:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
# Step 3: Print each term by manually calling the function (example for 5 terms)
print(fibonacci(1))
print(fibonacci(2))
print(fibonacci(3))
print(fibonacci(4))
print(fibonacci(5)) # Extend manually if more terms are needed
```
#### **Logic**
1. Initialize the first two terms of the sequence.
2. Use a `while` loop to print terms until the specified count is reached.
#### **Algorithm**
1. Start
2. Input the number of terms (`n`).
3. Initialize `a = 0`, `b = 1`, and `count = 0`.
4. While `count < n`:
- Print `a`
- Calculate the next term (`next_term = a + b`)
- Update `a` and `b` (`a = b` and `b = next_term`)
- Increment `count`
5. End
```python
# Step 1: Input the number of terms
terms = int(input("Enter the number of terms: "))
---
#### **Logic**
1. Input the number of terms.
2. Initialize the first two terms.
3. Use a `for` loop to iterate and print each Fibonacci term.
#### **Algorithm**
1. Start
2. Input the number of terms (`n`).
3. Initialize `a = 0` and `b = 1`.
4. For `i` in range from 0 to `n - 1`:
- Print `a`
- Calculate the next term (`next_term = a + b`)
- Update `a` and `b` (`a = b` and `b = next_term`)
5. End
```python
# Step 1: Input the number of terms
terms = int(input("Enter the number of terms: "))
---
Each method prints the Fibonacci sequence up to the specified number of terms,
using different programming structures.