Fibonacci Series Using For Loop in C++
Q. Write a C++ program for Fibonacci Series using for loop.
#include <iostream>
using namespace std;
int main()
{
int n1=0,n2=1,n3,n;
cout << "Enter the number:";
cin >> n;
cout << "Fibonacci series terms are: " << n1 << " " << n2;
for(int i=2; i<n; i++)
{
n3=n1+n2;
n1=n2;
n2=n3;
cout << " " << n3 ;
}
return 0;
}
Q. Write an algorithm to Fibonacci Series using for loop.
1. Start 2. Initialize: - Set `n1 = 0` (first term) - Set `n2 = 1` (second term) 3. Read the input number `n` (number of terms to print). 4. Print the first two terms: `n1` and `n2`. 5. Loop from `i = 2` to `i < n`: a. Calculate `n3 = n1 + n2` (next term in the series). b. Print `n3`. c. Update `n1 = n2`. d. Update `n2 = n3`. 6. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com