We have studied a lot about loops and their types in our previous posts. Besides, we also studied the demonstration of different types of loops with different example programs. In this short and brief article, we will demonstrate nested loops with an example c++ program to print the Fibonacci series incrementally using a nested loop. The main purpose of writing is to make you understand when and why to write nested loop? How to write a nested loop?
C++ program to print Fibonacci series incrementally using nested loop
In order to demonstrate nested loops, I am going to write a basic c++ program to print the Fibonacci series incrementally using nested loop. The program should first ask the user to enter a number. Then, it should print the first term of the Fibonacci series in the first line, second-term in the second line, third in the third line, and so on up to the number entered by the user. Before starting the guide, we must know what is the Fibonacci series. Let's see what it is?
What is the Fibonacci series?
In the Fibonacci series, the first two terms are always 0 and 1. Next to the first two terms, all the terms are the sum of the preceding(previous) two terms. Let's understand the first five terms of the Fibonacci series.e.g 0,1,1,2,3. As said earlier the first two terms are always 0 and 1. The third term is the sum of the first and second term.e.g. (0+1=1). Similarly, the fourth term is the sum of the second and third term.e.g(1+1=2). likewise, the fifth term is also the sum of the third and fourth term.e.g(1+2=3). The same sequence continues up to n number of terms.
Now we know about the Fibonacci series and our problem statement. We are ready to go for the code.
#include<iostream> using namespace std; int main() { int n, prevnum, currentnum, nextnum; cout<<"Enter a number: "; cin>>n; for (int j = 0; j <n; j++) { prevnum = 0; currentnum = 1; for (int i = 0; i <= j; i++) { cout <<" " <<prevnum<<" "; nextnum = prevnum + currentnum; prevnum = currentnum; currentnum = nextnum; } cout << endl; } system("pause"); return 0; }
If we take a look at the code above, we can understand that we have variable n, prevnum, currentnum and nextnum. Variable n is declared to take input from the user. Prevnum and currentnum are variables that store the two preceding terms. Next num is the sum of the preceding two numbers. Here is the stepwise explanation of the code. Let's see what we did in the above code.
- First of all, we took input from the user in variable n.
- Then we wrote a nested loop. The outer loop is restricted to iterate n time which is the input of the user.e.g(5 iterations if the user enters 5)
- Inside the body of the outer loop, prevnum and currentnum are initialized with 0 and 1 because the first two terms of the Fibonacci series are always 0 and 1.
- Then, we wrote the inner loop that will iterate j times. It means that the inner loop will iterate until the value of I<=j.
- Inside the body of the inner loop, we printed prevnum as a term of the Fibonacci series.
- Then, we calculated the next term of the Fibonacci series by adding prevnum and currentnum.
- Now prevnum is overwritten with the value of currentnum to print it in the next iteration.
- In the same way, currentnum is overwritten with the value of the nextnum.
- Then we wrote a cout statement for a line break.
- Here is the end of the first iteration of the outer loop. All the above steps will repeat in the next iteration.
Nested loops are helpful when you want to repeat some actions on every pass of the outer loop. In our example, we are printing terms of the Fibonacci series on every execution of the outer loop. In the above example, the outer loop is creating a sequence of execution for the inner loop while the inner loop is printing terms of the Fibonacci series. Hence, we are executing two interdependable iterations. That's all related to nested loops and c++ program to print the Fibonacci series incrementally using nested for loop. We have demonstrated nested loops with for loop but you can nest other loops in the same way. thanks for reading and supporting us.
0 Comments