- C++ Programming Examples
- C++ Programming Examples
- C++ Hello World
- C++ Get Input
- C++ Print Integer
- C++ Add Two Numbers
- C++ Add Sub Mul Div
- C++ Add Digits
- C++ Find Average Perc
- C++ Find Arithmetic Mean
- C++ Sum of n Natural Numbers
- C++ Sum of n Numbers
- C++ Area Perimeter of Square
- C++ Area Perimeter of Rectangle
- C++ Area Perimeter of Triangle
- C++ Area Circum of Circle
- 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 Number
- C++ Find Factors of Number
- C++ Find HCF & LCM
- C++ Make Calculator
- C++ Count Digits in Number
- C++ Sum of First & Last Digit
- C++ Product of Digits of Number
- 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
- Check Reverse equal Original
- C++ Check Perfect Number
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Divisibility Test
- C++ Find Wage of Labor
- 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
- Find Largest Element in Array
- Find Smallest Element in Array
- Find Second Largest Element
- Find Second Smallest Element
- C++ Sum of All Elements
- C++ Multiply All Elements
- C++ Element on Even Position
- C++ Element on Odd Position
- C++ Print Even Numbers in Array
- C++ Print Odd Numbers in Array
- C++ Count Even/Odd Numbers
- C++ Sum of Even/Odd Numbers
- C++ Count Positive Negative Zero
- C++ Reverse an Array
- C++ Insert Element in Array
- C++ Delete Element from Array
- 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 Length of String
- C++ Compare Two Strings
- C++ Copy String
- C++ Concatenate String
- C++ Reverse a String
- C++ Delete Vowels from String
- C++ Delete Word from String
- C++ Count Character in String
- C++ Count Word in String
- C++ Frequency of Word
- C++ Remove Spaces from String
- C++ Sort a String
- C++ Uppercase to Lowercase
- C++ Lowercase to Uppercase
- C++ Swap Two Strings
- C++ Check Anagram or Not
- C++ Capitalize All Words in String
- C++ Capitalize Specific Character
- C++ Get Numbers from String
- C++ File Programs
- C++ Read a File
- C++ Write Content to File
- C++ Append Data in File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- Count Characters, Words in File
- C++ Capitalize All Words in File
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Misc Programs
- C++ Print ASCII Value
- C++ Add Binary Numbers
- C++ Generate Random Numbers
- C++ Print Smiling Face
- C++ Days into Years, Months
- Add Two Numbers using Pointer
- C++ Print Fibonacci Series
- Generate Armstrong Numbers
- C++ Find nCr and nPr
- C++ Get IP Address
- C++ Print Date/Time
- C++ Shutdown, Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Program to Print Fibonacci Series
In this article, you will learn and get code to print Fibonacci series using C++ program in following ways:
- Print Fibonacci Series of N Terms
- Print Fibonacci Series upto Given Number
- Print Fibonacci Series using Function. This program includes both ways of printing Fibonacci series
In first program, if user enters a number say 10 as input, then the program prints Fibonacci series with 10 terms like:
0 1 1 2 3 5 8 13 21 34
Whereas in second program, if user enters 10 as input, then the program prints Fibonacci series in which the last number can not be greater than 10. Here is the Fibonacci series upto 10:
0 1 1 2 3 5 8
In third program, we've implemented both features using switch case and user-defined functions.
But before going through these programs, let's understand about Fibonacci series.
What are Fibonacci Series ?
Fibonacci series is a sequence of numbers in which the first two numbers starts with 0 and 1. And all the upcoming numbers gets calculated as the summation of its previous two numbers in following ways:
- 0 as first number
- 1 as second number
- 1 as third number. Third number is the summation of second (1) and first (0) number. That is, 1+0
- 2 as fourth number. Fourth number is the summation of third (1) and second (1) number. That is, 1+1
- 3 as fifth number. That is, 2+1
- 5 as sixth number. That is, 3+2
- and so on
Print Fibonacci Series of N Terms
To print Fibonacci series of N terms in C++ programming, you have to ask from user to enter the size of Fibonacci sequence or series. And then generate and print Fibonacci series of given size as shown in the program given below:
#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 build and run under Code::Blocks IDE. Here is its sample run:
Now supply the input as number to print Fibonacci series. Here the number indicates number of digits or terms (whatever you say), Fibonacci series contains. For example, if user enters 10 as input, then the program prints Fibonacci series that contains 10 digits or numbers. Here is the sample output with user input, 10:
Print Fibonacci Series upto Given Number
This program generates Fibonacci series in a way that the last number can't be greater than the number entered by user as shown in the program given below.
The question is, write a program in C++ to print Fibonacci series upto 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 term and upto given number both, whatever user wants to perform using menu-driven feature. This program uses user-defined functions namely FiboOfNTerm() and FiboUptoGivenNumber() to print Fibonacci series in both ways.
#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 Fibonacci series. Here is the sample output produced
after entering 1 as choice and pressing ENTER
key:
Enter the value of N to generate and print Fibonacci series of N numbers. Here is its sample output after supplying or entering 10 as value of N:
As you can see that the Fibonacci series of 10 terms gets printed and the choice again gets displayed to continue the operation. Let's check it out with second and then third option as shown in the snapshot given below:
Same Program in Other Languages
« Previous Program Next Program »