- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- C++: Find Simple Interest
- C++: Fahrenheit to Celsius
- C++: Celsius to Fahrenheit
- C++: Print Prime Numbers
- C++: Reverse a Number
- C++: Swap Two Numbers
- C++: Print Multiplication Table
- C++: Find Factorial of a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- C++: Sum of Squares of Digits
- C++: Interchange Digits of Number
- C++ if-else Programs
- C++: Check Even or Odd
- C++: Check Prime or Not
- C++: Check Alphabet or Not
- C++: Check Vowel or Not
- C++: Check Leap Year or Not
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- C++: Find Discounted Price
- C++: Find Shipping Charge
- C++: Find Telephone Bills
- C++: Calculate Student Grade
- C++: Largest of Two Numbers
- C++: Largest of Three Numbers
- C++ Number Conversion
- C++: Decimal to Binary
- C++: Decimal to Octal
- C++: Decimal to Hexadecimal
- C++: Binary to Decimal
- C++: Binary to Octal
- C++: Binary to Hexadecimal
- C++: Octal to Decimal
- C++: Octal to Binary
- C++: Octal to Hexadecimal
- C++: Hexadecimal to Decimal
- C++: Hexadecimal to Binary
- C++: Hexadecimal to Octal
- C++ Pattern Programs
- C++: Pattern Programs
- C++: Print Diamond Pattern
- C++: Print Floyd's Triangle
- C++: Print Pascal's Triangle
- C++ Array Programs
- C++: 1D Array Program
- C++: Linear Search
- C++: Binary Search
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- C++: Merge two Arrays
- C++: Bubble Sort
- C++: Selection Sort
- C++: Insertion Sort
- C++: Common Elements
- C++: 2D Array Programs
- C++: Add Two Matrices
- C++: Subtract Two Matrices
- C++: Transpose Matrix
- C++: Multiply Two Matrices
- C++: 3D Array Programs
- C++ String Programs
- C++: Print String
- C++: Find String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ program to print the Fibonacci series
In this article, you will learn and get code to print Fibonacci series using a C++ program in the following ways:
- Print the Fibonacci Series of N Terms
- Print the Fibonacci Series up to the given number
- Print the Fibonacci Series Using Function. This program includes both ways of printing the Fibonacci series.
In the first program, if the user enters a number, say 10, as input, then the program prints the Fibonacci series with 10 terms like:
0 1 1 2 3 5 8 13 21 34
Whereas in the second program, if the user enters 10 as input, then the program prints a Fibonacci series in which the last number can not be greater than 10. Here is the Fibonacci series up to 10:
0 1 1 2 3 5 8
In the third program, we've implemented both features using switch cases and user-defined functions.
But before going through these programs, let's understand the Fibonacci series.
What is the Fibonacci series?
The Fibonacci series is a sequence of numbers in which the first two numbers start with 0 and 1. And all the upcoming numbers get calculated as the sum of their previous two numbers in the following ways:
- 0 as the first number
- 1 as the second number
- The third number is 1. The third number is the sum of the first (0) and second (1) numbers. That is, 1+0.
- The fourth number is 2. The fourth number is the sum of the third (1) and second (1) numbers. That is, 1+1.
- The fifth number is 3. That is, 2+1.
- The sixth number is 5. That is, 3+2.
- and so on.
Print the Fibonacci Series of N Terms
To print a Fibonacci series of N terms in C++ programming, you have to ask the user to enter the size of the Fibonacci sequence or series. Then, as shown in the program below, generate and print a Fibonacci series of the given size:
#include<iostream> using namespace std; int main() { int a=0, b=1, c, tot, temp, i; cout<<"Enter the Size of Fibonacci Sequence: "; cin>>tot; cout<<"\nFibonacci Series of "<<tot<<" Terms:\n"; for(i=1; i<=tot; i++) { if(i==1) c = 0; else if(i==2 || i==3) c = 1; else { temp = a; a = b; b = c; c = a+b; } if(i==tot) cout<<c; else cout<<c<<", "; } cout<<endl; return 0; }
This program was built and runs under the Code::Blocks IDE. Here is its sample run:
Now supply the input as a number to print the Fibonacci series. Here the number indicates the number of digits or terms (whatever you say) the Fibonacci series contains. For example, if the user enters 10 as input, then the program prints a Fibonacci series that contains 10 digits or numbers. Here is the sample output with user input: 10
Print the Fibonacci Series up to the given number
This program generates Fibonacci numbers in such a way that the last number can't be greater than the number entered by the user, as shown in the program given below.
The question is: write a program in C++ to print Fibonacci numbers up to a given number. Here is its answer:
#include<iostream> using namespace std; int main() { int a=0, b=1, c, limit, temp, i; cout<<"Enter the Limit: "; cin>>limit; cout<<"\nFibonacci Series upto "<<limit<<":\n"; for(i=1; ; i++) { if(i==1) c = 0; else if(i==2 || i==3) c = 1; else { temp = a; a = b; b = c; c = a+b; } if(c>limit) break; cout<<c<<" "; } cout<<endl; return 0; }
Here is its sample run with user input, 200:
Fibonacci Series using Function
This program generates and prints Fibonacci series of N terms and up to a given number for both, depending on what the user chooses from the menu-driven feature. This program prints the Fibonacci series in both ways using user-defined functions FiboOfNTerm() and FiboUptoGivenNumber().
#include<iostream> using namespace std; void FiboOfNTerm(int); void FiboUptoGivenNumber(int); int main() { int ch, N, limit; do { cout<<"1. Fibonacci Series of N Term\n"; cout<<"2. Fibonacci Series upto Given Number\n"; cout<<"3. Exit\n"; cout<<"Enter Your Choice: "; cin>>ch; switch(ch) { case 1: cout<<"\nEnter the Value of N: "; cin>>N; FiboOfNTerm(N); break; case 2: cout<<"\nEnter the Number (Limit): "; cin>>limit; FiboUptoGivenNumber(limit); break; case 3: return 0; default: cout<<"\nWrong Input!"; break; } cout<<"\n\n"; }while(ch==1 || ch==2); cout<<endl; return 0; } void FiboOfNTerm(int tot) { int i, a=0, b=1, c, temp; for(i=1; i<=tot; i++) { if(i==1) c = 0; else if(i==2 || i==3) c = 1; else { temp = a; a = b; b = c; c = a+b; } cout<<c<<" "; } } void FiboUptoGivenNumber(int limit) { int i, a=0, b=1, c, temp; for(i=1; ; i++) { if(i==1) c = 0; else if(i==2 || i==3) c = 1; else { temp = a; a = b; b = c; c = a+b; } if(c>limit) break; cout<<c<<" "; } }
Here is its sample run. This is the initial output:
Now enter your choice, that is, in what way you want to print the Fibonacci series. Here is the sample output produced after entering 1 as a choice and pressing the ENTER key:
Enter the value of N to generate and print the Fibonacci series of N numbers. Here is its sample output after supplying or entering 10 as the value of N:
As you can see, the Fibonacci series of 10 terms gets printed, and the choice again gets displayed to continue the operation. Let's check it out with the second and then third option, as shown in the snapshot given below:
The same program in different languages
« Previous Program Next Program »