CS - Program to generate Fibonacci Series
Write a program to generate a Fibonacci series up to a given number using C++?
First of all we need to know - What is Fibonacci Series?
It is a series of numbers that follows some pattern generated by adding previous two numbers, where first two numbers are 0 and 1. Fibonacci series has lots of uses in our life, you can easily find them on internet, just by googling it. But I have kind brought them on a single page - Click here to get more information about Fibonacci series.
C++ Program for generating Fibonacci Series (using while loop):
#include<iostream.h>
void main()
{
int N, a=0,b=1,c=a+b;
clrscr();
cout<<"\nEnter the Upper Limit: ";
cin>>N;
cout<<a<<" "<<B<<" ";
while(c<=N)
{
cout<<c<<" "
a=b;
b=c;
c=a+b;
}
}
C++ Program for generating Fibonacci Series (using for loop):
#include<iostream.h>
void main()
{
int N, a=0,b=1,c=0;
clrscr();
cout<<"\nEnter the Upper Limit: ";
cin>>N;
cout<<a<<" "<<B<<" ";
for(c=a+b;c<=N;c=a+b)
{
cout<<c<<" "
a=b;
b=c;
}
}
More useful links:
Comments
Post a Comment