Saturday, November 29, 2014

Fibonacci Series & prime number in C++



Swap two variables without using third variable

#include
#include
int main()
{
int var1, var2;
 cout<<"enter value for first integer:  ";
 cin>>var1;
 cout<<"enter value for second integer:  ";
 cin>>var2;
 cout<<" values before swapping:  "<  cout<<"first integer ="<  cout<<"second interger ="<
              var1=var1+var2;
              var2=var1-var2;
              var1=var1-var2;
 cout<<" values after swapping:  "<  cout<<"first integer ="<  cout<<"second interger ="<     return 0;

}
Find last prime number occur before enter number

#include
#include
C++ program to find Fibonacci Series upto given range
    #include
    #include
   int main()
    {
       int range, first = 0, second = 1, fibonicci=0;
       cout << "enter range for terms of fibonacci sequence: ";
       cin >> range;
       cout << "fibonicci series upto " << range << " terms "<< endl;
       for ( int c = 0 ; c < range ; c++ )
       {
          if ( c <= 1 )
             fibonicci = c;
          else
          {
             fibonicci = first + second;
             first = second;
             second = fibonicci;
          }
          cout << fibonicci <<" ";
       }
       return 0;
    }
C++ code which take two numbers from user and swap using built in swap function
#include
 #include
void swap()
{
    cout<<"this is my swap";
}
int main()
{
    int firstnum , secondnum;
    cout<<"enter value for first number:   ";
    cin>>firstnum;
    cout<<"enter value for second number:  ";
    cin>>secondnum;
    cout<<"\n\n";
    cout<<"values before calling built in swap function"<
    cout<<"\tfirst  number = "<
    cout<<"\tsecond number = "<
     cout<<"\n\n";
     std::swap( firstnum , secondnum );
    cout<<"values after calling bulit in  swap function"<
    cout<<"\tfirst number = "<
    cout<<"\tsecond number = "<
    return 0;
}         

No comments:

Post a Comment