Monday, 16 February 2015

Function In c++(practicla 2)

Practical -2

  •  Wite a program to demonstrate inline function.
#include<iostream>
using namespace std;

inline float max1(float a,float b);
int main()
{
    float a,b;
    float result;
    cout<<"write the value of a";
    cin>>a;
    cout<<"write the value of b";
    cin>>b;
     result=max1(a,b);
    cout<<"max("<<a<<","<<b<<"):  "<<endl;
    cout<<result<<endl;

    return(0);

}
inline float max1(float a,float b)
{
    return(  (a>b)? a:b );
}

  • Wite a program to demonstrate overloading of function. 

#include<iostream>
using namespace std;
void test(int);           //function 1
void test(int ,float);   //function 2
void test(float);        //function 3
int main()
{

    int a=50;
    float b=23.26;
    test(a);   //call fun1
    test(b);    //call fun3
    test(a,b);  //call fun2

    return(0);


}

void test(int a )
{
    cout<<"Integer number: "<<a<<endl;
}

void test(float var)
{
    cout<<"Float number: "<<var<<endl;
}

void test(int var1, float var2)
{
    cout<<"Integer number: "<<var1<<endl;
    cout<<" And float number:"<<var2<<endl;
}
 

Tuesday, 10 February 2015

C++ code for 1st practical (intoduction to c++)

         SEMESTER 4: Object oriented programming with C++.



.check that given number is palindron or not :c++ code
 #include<iostream>
using namespace std;
int main()
{
  int number,temp;
  int sum=0,reminder;
  cout<<"give the number what you want"<<endl;
  cin>>number;
  temp=number;
  while(number !=0)
  {
      reminder=number%10;
      sum=sum+reminder*reminder*reminder;
      number=number/10;

  }
    if(sum == temp)
        cout<<"armstrong number"<<endl;
    else
         cout<<"not armstrong number"<<endl;


   return(0);

}

  • find the maximum among the three number
#include<iostream>
using namespace std;
int main()
{
     int a,b,c;
     cout<<"give your three numbers"<<endl;
     cout<<"value of a"<<endl;
     cin>>a;
     cout<<"value of b"<<endl;
     cin>>b;
     cout<<"value of c"<<endl;
     cin>>c;

     if(a>b)
     {
         if(a>c)
            cout<<a <<'\t'<<" is maximum among all three"<<endl;
         else
            cout<<c <<'\t'<<" is maximum among all three"<<endl;
     }

     if (b>a)
     {
         if(b>c)
            cout<<b <<'\t'<<"  is maximum among all three"<<endl;
         else
            cout<<c <<'\t'<<"  is maximum among all three"<<endl;
     }

         cout<<"*******"<<endl;
  
  return(0);

}

  • write a code for Fibonacci series up to 100.
#include<iostream>
using namespace std;
int main()
{
    int a=0,b=1,c=0;
    int i;
    cout<<"fibbo series of 1 to 100"<<endl;
    for(i=0;i<30;i++)
        {

             if(i<=1)
              c=i;
              else
               {
                   c=a+b;
                    a=b;
                    b=c;
                }

            if(c<=100)
            {
                cout<<c<<endl;
            }

         }
    return(0);
}
  • write a program to exchange the information of two variable using pass by reference.

    #include<iostream>
    using namespace std;
    int  swap(float &a,float &b);
    int main()
    {
        float a,b;
        cout<<"give the value of two variable"<<endl;
        cout<<"value of a is "<<endl;
        cin>>a;
        cout<<"value of b is "<<endl;
        cin>>b;
        swap(a,b);
         cout<<" 1.value of a is"<<a<<endl;
          cout<<" 1.value of b is"<<b<<endl;
    }
    int swap(float &a,float &b)
    {
        float t;
          t=a;
          a=b;
          b=t;
         return(0);

    }