0% found this document useful (0 votes)
9 views

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

Uploaded by

gspatnaik26
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

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

Uploaded by

gspatnaik26
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

To print the Fibonacci series up to a specified number of terms, we can use

different approaches. The Fibonacci sequence starts with 0 and 1, and each
subsequent term is the sum of the previous two terms.

---

### **1. Without Using Any Loop or Conditional Statements**

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 Program**

```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 2: Input the number of terms


terms = int(input("Enter the number of terms: "))

# 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
```

#### **Expected Output**


If the user inputs 5 terms:
```
Enter the number of terms: 5
0
1
1
2
3
```
---

### **2. Using a `while` Loop**

#### **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 Program**

```python
# Step 1: Input the number of terms
terms = int(input("Enter the number of terms: "))

# Step 2: Initialize the first two terms


a, b = 0, 1
count = 0

# Step 3: Print Fibonacci series using while loop


while count < terms:
print(a)
next_term = a + b
a, b = b, next_term
count += 1
```

#### **Expected Output**


If the user inputs 5 terms:
```
Enter the number of terms: 5
0
1
1
2
3
```

---

### **3. Using a `for` Loop**

#### **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 Program**

```python
# Step 1: Input the number of terms
terms = int(input("Enter the number of terms: "))

# Step 2: Initialize the first two terms


a, b = 0, 1

# Step 3: Print Fibonacci series using for loop


for _ in range(terms):
print(a)
next_term = a + b
a, b = b, next_term
```

#### **Expected Output**


If the user inputs 5 terms:
```
Enter the number of terms: 5
0
1
1
2
3
```

---

Each method prints the Fibonacci sequence up to the specified number of terms,
using different programming structures.

You might also like