Generate Fibonacci Series
C++
Medium
3 views
Problem Description
Print series where each number is sum of previous two: 0,1,1,2,3,5,8...
Real Life: This pattern appears in nature like flower petals, pine cones.
Step-by-Step Logic:
1. Start with first two numbers: 0 and 1
2. Print these two
3. Next number = sum of previous two
4. Update previous two numbers
5. Repeat N times
Official Solution
void control_q10_fibonacci() {
int terms = 10;
int first = 0, second = 1;
cout << "Fibonacci series: ";
cout << first << " " << second << " ";
for(int i = 3; i <= terms; i++) {
int next = first + second;
cout << next << " ";
first = second;
second = next;
}
cout << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!