### Learn c++ - swapping and fibonacci sequence

Fibonacci Sequence is defined as follows : the first and second terms in the sequence are 0 and 1. subsequent terms are found by adding the preceding two terms in the sequence . For example : 0 1 1 2 3 5 8  13

Swapping is to interchange the value of two Variable

Suppose we assign  0 and 1 values to variable x and y respectively then on swapping we need to have x=1 and y=0.

Program to Swap values:

#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();

int x,y,temp;
x=1;
y=2;

temp=x;
x=y;
y=temp;

cout<<"X= "<<x<<endl<<"Y ="<<y;
getch();
}

Explanation: Here first we assign value of X and Y to be 1 and 2 respectively . then we say program to store the value of X into a variable temp (which is unassigned earlier, then we assign value of y in x and value of temp in y.

Program to generate fibonacci series:

We will be using swapping method implemented above to generate fibonacci series

#include<iostream.h>
#include<conio.h>
void main()
{

clrscr();
int first,second,third,m;
first=0;
second=1;
cout<<"How many elements you want :"<<endl;
cin>>m;
cout <<first<<" "<<second;

for(int i=2;i<n;i++)
{third=first+second;
cout<<" "<<third;
first=second;
second=third;
}
getch();
}

Explanation : Syntax "for(int i=2;i<n;i++)" will initialize a loop starting from i=2 and less than n terms which will increment till n terms. this program will goes on repeating the code till n term and will swap First , second and third term in such a way that third term will be sum of First and second term