Topics

6/recent/ticker-posts

Some important programs part 3

Some important programs part 3

11. Series of Even and Odd Numbers using scope resolution operator:

#include<iostream>

using namespace std;

class EvenOdd

{

private:

    int n,i;

public:

    void get();

    void print();

};

void EvenOdd::get()

{

    cout<<"Enter the values of n:"<<endl;

    cin>>n;

}

void EvenOdd::print()

{

    cout<<"Series of Even Numbers"<<endl;

    for(i=0;i<n;i=i+2)

        cout<<i<<endl;

    cout<<"Series of Odd Numbers"<<endl;

    for(i=1;i<=n;i=i+2)

        cout<<i<<endl;

}

int main()

{

    EvenOdd eo;

    eo.get();

    eo.print();

    return 0;

}

12. This Pointer: 

#include<iostream>

using namespace std;

class Temp 

{

private:

    int a;

public:

    void test()

    {

        this->a=14;

        cout<<"Value of a[Using This Pointer is]"<<this->a<<endl;

    }

};

int main()

{

    Temp t;

    t.test();

return 0;

}

13. Unary Operator

#include<iostream>

using namespace std;

class UnaryOPE

{

private:

    int p,s;

public:

    void get()

    {

        cout<<"Enter the value of P,S"<<endl;

        cin>>p>>s;

    }

    void operator++()

    {

        ++p;

        ++s;

    }

    void display()

    {

        cout<<"P="<<p<<endl;

        cout<<"S="<<s<<endl;

    }

};

int main()

{

    UnaryOPE we;

    we.get();

    cout<<"Original value"<<endl;

    we.display();

    cout<<"Incremented values"<<endl;

    ++we;

    we.display();

    return 0;

}

14. New and Delete Operator:

#include<iostream>
using namespace std;
int main()
{
    int x=1,sum=0;
    cout<<"Enter the Size of Dynamic Array "<<endl;
    cin>>x;
    while(x<=0)
    {
        cout<<"Invalid Entry! Try again"<<endl;
        cin>>x;
    }
    int *p = new int [x];
    for(int i=0; i<x; i++)
    {
        cout<<"Enter the value for p["<<i<<"]:"<<endl;
        cin>>p[i];
    }
    cout<<"the numbers are"<<endl;
    for(int i=0;i<x;i++)
        cout<<p[i]<<endl;
        cout<<"The sum of the numbers"<<endl;
    for(int i=0;i<x;i++)
        sum = sum +p[i];
        cout<<sum<<endl;
    delete []p;
    return 0;
}

 

15. Print the Number of days in a month using switch case statement 

#include<iostream>
using namespace std;
int main()
{
    int monthnumber;
    cout<<"Enter the Month Number"<<endl;
    cin>>monthnumber;
    switch(monthnumber)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
        cout<<monthnumber<<"has 31 Days"<<endl;
    break;
        case 2:
            cout<<monthnumber<<"Has 28 days"<<endl;
     break;
     case 4:
     case 6 :
     case 9 :
     case 11:
        cout<<monthnumber<<"has 30 days"<<endl;
        break;
    }
    return 0;
}

 

                            ***********************************************

Post a Comment

0 Comments