HOME

Tuesday, September 22, 2015

EXAMPLE FOR FRIEND FUNCTION IN C++

#include<iostream>
using namespace std;
class sample
{
    int a,b;
public:
    void getdata()
    {
        cout<<"Enter the value of a and b"<<endl;
        cin>>a>>b;
    }
    friend int mean(sample s)
    {
        int m=(s.a+s.b)/2;
        return m;
    }
};
int main()
{
    sample s;
    s.getdata();
    cout<<" mean = "<<mean(s);
return 0;
}

SUM OF DISTANCE BY PASSING OBJECT AS ARGUMNET IN C++

#include<iostream>
using namespace std;
class distancee
{
    int km,m,cm;
public:
    void getdata();
    void sum(distancee, distancee);
};
void distancee :: getdata()
{
    cout<<"Enter the kilometer"<<endl;
    cin>>km;
    cout<<"Enter the meter"<<endl;
    cin>>m;
    cout<<"Enter the centimeter"<<endl;
    cin>>cm;
}
void distancee :: sum(distancee d1, distancee d2)
{
    cm=d1.cm+d2.cm;
    m=cm/100;
    cm%=100;
    m=m+d1.m+d2.m;
    km=m/1000;
    m%=1000;
    km=km+d1.km+d2.km;
    cout<<"total distance is "<<endl;
cout<<km<<" km "<<m<<" m "<<cm<<" cm "<<endl;
}
int main()
{
    distancee d1,d2,d3;
    d1.getdata();
    d2.getdata();
    d3.sum(d1,d2);
    return 0;
}

CALCULATING TIME BY PASSING ONJECT AS THE FUNCTION ARGUMENT

#include<iostream>
using namespace std;
class time
{
    int hour, minute, second;
public:
    void getdata()
    {
        cout<<"Enter the hour"<<endl;
        cin>>hour;
        cout<<"Enter the minute"<<endl;
        cin>>minute;
        cout<<"Enter the second"<<endl;
        cin>>second;
    }
    void display(time t1, time t2)
    {
        second=t1.second+t2.second;
        minute=second/60;
        second%=60;
        minute=minute+t1.minute+t2.minute;
        hour=minute/60;
        minute%=60;
        hour=hour+t1.hour+t2.hour;

        cout<<"The total time is "<<endl;
        cout<<hour<<" hour "<<minute<<" minute "<<second<<" second "<<endl;
    }
};
int main()
{
    time t1,t2,t3;
    t1.getdata();
    t2.getdata();
    t3.display(t1,t2);
    return 0;
}

Monday, September 7, 2015

FIBONICCI SERIES USING RECURSION IN C++

#include<iostream>
using namespace std;
int fibo(int, int, int);
int main()
{
    int n;
    cout<<"Enter the number"<<endl;
    cin>>n;
    fibo(0,1,n);
return 0;
}
int fibo(int a, int b, int n)
{
    int c;
    if(n==0)
    {
        return 0;
    }
    else{
        cout<<a<<"\t";
        c=a+b;
        a=b;
        b=c;
    return fibo(a,b,(n-1));
    }
}

FUNCTION OVERLOADING SAMPLE PROGRAM

#include<iostream>
using namespace std;
int volume(int);
int volume(int, int, int);
void volume(float, float);
int main()
{
    int s;
    cout<<"Enter size of cube"<<endl;
    cin>>s;
    cout<<"Volume of cube is "<<volume(s)<<endl;
    int l,b,h;
    cout<<"Enter the length, height and width of cuboid"<<endl;
    cin>>l>>b>>h;
    cout<<"Volume of cuboid is "<<volume(l,b,h)<<endl;
    float r,h1;
    cout<<"Enter the radius and height of cylinder"<<endl;
    cin>>r>>h1;
    volume(r,h1);
    return 0;
}
int volume(int l)
{
    return l*l*l;
}
int volume(int l,int b,int h)
{
    return (l*b*h);
}
void volume(float r, float h)
{
    float result;
    result=3.14*r*r*h;
    cout<<"Volume of cylinder is "<<result<<endl;
}

FIBONICCI SERIES USING RECURSION IN C++

#include<iostream>
using namespace std;
int fibo(int, int, int);
int main()
{
    int n;
    cout<<"Enter the number"<<endl;
    cin>>n;
    fibo(0,1,n);
return 0;
}
int fibo(int a, int b, int n)
{
    int c;
    if(n==0)
    {
        return 0;
    }
    else{
        cout<<a<<"\t";
        c=a+b;
        a=b;
        b=c;
    return fibo(a,b,(n-1));
    }
}

Friday, September 4, 2015

COUNT TOTAL NO. OF DIGIT AND EVEN DIGIT PRESENT IN NUMBER

#include<iostream>
using namespace std;
int main()
{
    int num,rem,c=0,dig=0;
    cout<<"Enter the number"<<endl;
cin>>num;
while(num>0)
{
    rem=num%10;
    c++;
    if(rem%2==0)
    {
        dig++;
    }
    num/=10;
}
cout<<"number of digit present is "<<c<<endl;
cout<<"number of even digit present is "<<dig<<endl;
return 0;
}

TAKE INPUT AND DISPLAY DETAIL OF EMPLOY USING CLASS IN C++

#include<iostream>
using namespace std;
class employ
{
    char name[20];
    int id;
    double salary;
public:
    void getdata();
    void display();
};
void employ :: getdata()
{
    cout<<"Enter the name of the employ"<<endl;
    cin>>name;
    cout<<"Enter the ID of employ"<<endl;
    cin>>id;
    cout<<"Enter the salary of the employ"<<endl;
    cin>>salary;
}
void employ :: display()
{
    cout<<name<<"\t"<<id<<"\t"<<salary<<endl;
}
int main()
{
    employ a,b;
    a.getdata();
    b.getdata();
    cout<<"\nNAME\t"<<"ID\t"<<"SALARY"<<endl;
    a.display();
    b.display();
return 0;
}