๐
Date: Jan 29, 2026
๐ฅ Topic: Logic Building: Fibonacci Series
๐ The Sequence of Nature
The Fibonacci series is a sequence where the next number is the sum of the previous two numbers.
0, 1, 1, 2, 3, 5, 8, 13, 21...
Logic: Next = (Prev) + (PrevPrev)
๐ Dry Run
Let's print first 5 terms:
- Start with a=0, b=1. Print them.
- next = 0+1 = 1. (Update: a=1, b=1)
- next = 1+1 = 2. (Update: a=1, b=2)
- next = 1+2 = 3. (Update: a=2, b=3)
๐ป Day 29 Code
#include <iostream>
using namespace std;
int main() {
int n = 10; // Number of terms
int t1 = 0, t2 = 1;
int nextTerm;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
// Print the first term
cout << t1 << " ";
// Calculate next
nextTerm = t1 + t2;
// Update previous terms for next round
t1 = t2;
t2 = nextTerm;
}
return 0;
}
๐ญ Thoughts
This was tricky to implement at first because updating `t1` and `t2` has to be done in the correct order. This series appears everywhere in nature, from sunflowers to galaxies!
No comments:
Post a Comment