Wednesday, 31 July 2013

Constructor Arguments

Program to take three constructor taking into arguments and initializing values

#include<iostream.h>
#include<conio.h>
class complex
{
    float x,y;
    public:
        complex(){};
        complex(float a)
        {
            x=y=a;
        }
        complex(float real,float image)
        {
            x=real;
            y=image;
        }
        friend complex sum(complex,complex);
        friend void show(complex);
};
complex sum(complex c1,complex c2)
{
    complex c3;
    c3.x=c1.x+c2.x;
    c3.y=c1.y+c2.y;
    return(c3);
}
void show(complex c)
{
    cout<<c.x<<"+j"<<c.y<<"\n";
}
int main()
{
    clrscr();
    complex A(2.7,3.5);
    complex B(1.6);
    complex C;
    C=sum(A,B);
    cout<<"A= ";
    show(A);
    cout<<"B= ";
    show(B);
    cout<<"C= ";
    show(C);

    complex P,Q,R;
    P=complex(2.5,3.9);
    Q=complex(1.6,2.5);
    R=sum(P,Q);
    cout<<"\n";
    cout<<"P= ";
    show(P);
    cout<<"Q= ";
    show(Q);
    cout<<"R= ";
    show(R);
    getch();
    return 0;
}

Here is the Output:)


Parent Constructor

#include<iostream.h>
#include<conio.h>
#include<string.h>
class emp
{
    int empno;
    char name[30];
    public:
        emp(){};
        emp(int pno,char pname[30])
        {
            empno=pno;
            strcpy(name,pname);
        }
        void getdetails()
        {
            cout<<"\n Enter The Number: ";
            cin>>empno;
            cout<<"\n Enter The Name: ";
            cin>>name;
        }
        void display()
        {
            cout<<"\n Number= "<<empno;
            cout<<"\n Name= "<<name;
        }
};
class author:public emp
{
    int publication;
    public:
        author(){};
        author(int no,char name[30],int pubs):emp(no,name)
        {
            publication=pubs;
        }
        void getdetails()
        {
            emp::getdetails();
            cout<<"\n publication: ";
            cin>>publication;
        }
        void display()
        {
            emp::display();
            cout<<"\n Publication= "<<publication<<endl;
        }
};
void main()
{
    clrscr();
    emp e1;
    e1.getdetails();
    e1.display();
    author a1;
    a1.getdetails();
    a1.display();
    author a2(20,"Ritu",3);
    a2.display();
    getch();
}

Here is the output :)

 

Program for constructor and distructor of the class date ♥

#include<iostream.h>
#include<conio.h>
class date
{
    private:
        int dd;
        int mm;
        int yy;
    public:
        date(int d,int m,int y)
        {
            dd=d;
            mm=m;
            yy=y;
        }
        void display()
        {
            cout<<"\n\t"<<dd<<"-"<<mm<<"-"<<yy;
        }
};
void main()
{
    clrscr();
    date d1(05,01,91);
    d1.display();
    getch();
}


Here Is the Output :)


Program for the use of constructor and function..

#include<iostream.h>
#include<conio.h>
class incm
{
    public:
        int i;
        incm()
        {
            i=0;
        }
        void add()
        {
            i++;
        }
        void sub()
        {
            i--;
        }
        void show()
        {
            cout<<"\n\t"<<i<<endl;
        }
};
void main()
{
    clrscr();
    incm c1,c2;
    c1.add();
    c1.add();
    c2.sub();
    c2.sub();
    c1.sub();
    c2.add();
    c1.show();
    c2.show();
    getch();
}

Here is the output :)


Saturday, 27 July 2013

Christmas tree constructer ♥

#include<iostream.h>
#include<conio.h>
class tree
{
    private:
        int m,n,i,j,k;
    public:
        tree(int,int);
        void display()
        {
            for(i=m;i<=n;i++)
            {
                for(k=m;k<=n-i;k++)
                {
                    cout<<" ";
                }
                for(j=m;j<=i;j++)
                {
                    cout<<" "<<"*";
                }
                cout<<"\n";
            }
        }
};
tree::tree(int x,int y)
{
    m=x;
    n=y;
}
void main()
{
    clrscr();
    tree t1(1,5);
    tree t2(1,5);
    tree t3(1,5);
    t1.display();
    t2.display();
    t3.display();
    getch();
}

Here is the output :)


Simple Example of Constructor (B)

#include <iostream.h>
#include<conio.h>
class Line
{
   public:
      void setLength(double len);
      double getLength();
      Line();

   private:
      double length;
};
Line::Line()
{
    cout<<"Object is being created"<< endl;
}

void Line::setLength( double len )
{
    length = len;
}

double Line::getLength()
{
    return length;
}
void main( )
{
    clrscr();
    Line line;
    line.setLength(6.0);
    cout << "Length of line : " << line.getLength() <<endl;
    getch();
}

Here is the output :)


Constructor Example using For Loop ♥

#include<iostream.h>
#include<conio.h>
class mahi
{
    int m,n,i,j,k;
    public:
    mahi(int,int);
    void display()
    {
        for(i=m;i<=n;i++)
        {
            for(k=m;k<=n-i;k++)
            {
                cout<<" ";
            }
            for(j=m;j<=i;j++)
            {
                cout<<" "<<i;
            }
            cout<<"\n";
        }
    }
};
mahi::mahi(int x,int y)
{
    m=x;
    n=y;
}
void main()
{
    clrscr();
    mahi m1(1,3);
    mahi m2(1,5);
    cout<<"\n\n OBJECT1\n";
    m1.display();
    cout<<"\n\n OBJECT2\n";
    m2.display();
    getch();
}


Here is the Output :)


Simple Example of Constructor (A)

#include<iostream.h>
#include<conio.h>
class mahi
{
    int m,n;
    public:
        mahi(int,int);
        void display()
        {
            cout<<"\n M= "<<m;
            cout<<"\n N= "<<n;
            cout<<"\n M+N= "<<m+n;
            cout<<"\n M-N= "<<m-n;
            cout<<"\n M*N= "<<m*n;
            cout<<"\n M/N= "<<m/n;
        }
};
mahi::mahi(int x,int y)
{
    m=x;
    n=y;
}
void main()
{
    clrscr();
    mahi m1(100,50);
    mahi m2(40,20);
    cout<<"\n\n OBJECT1\n";
    m1.display();
    cout<<"\n\n OBJECT2\n";
    m2.display();
    getch();
}


Here is the output :)


Simple Example of Constructor

#include<iostream.h>
#include<conio.h>
class mahi
{
    int m,n;
    public:
        mahi(int,int);
        void display()
        {
            cout<<"\n m= "<<m;
            cout<<"\n n= "<<n;
        }
};
mahi::mahi(int x,int y)
{
    m=x;
    n=y;
}
void main()
{
    clrscr();
    mahi m1(10,20);
    mahi m2(30,40);
    cout<<"\n\n OBJECT1\n";
    m1.display();
    cout<<"\n\n OBJECT2\n";
    m2.display();
    getch();
}

Here is the Output :)


Friday, 26 July 2013

Define Structure in C++

                   Although arrays greatly improved our ability to store data, there is one major drawback to their use ... each element (each box) in an array must be of the same data type.  It is often desirable to group data of different types and work with that grouped data as one entity.  We now have the power to accomplish this grouping with a new data type called a structure.
    

                    A structure is a collection of variable types grouped together. You can refer to a structure as a single variable, and to its parts as members of that variable by using the dot (.) operator.  The power of structures lies in the fact that once defined, the structure name becomes a user-defined data type and may be used the same way as other built-in data types, such as int, float,double and char.
 

****Defining a structure****


                    When dealing with the students in a college, many variables of different types
are needed.  It may be necessary to keep track of a name, an address (street, city, state, zip code), an age, an ID number, and a grade point average.

For example:-

struct STUDENT_TYPE
{
        char name, street, city, state, zipcode;
        int age;
        double IDnum;
        double grade;
};

Here, STUDENT_TYPE is called the structure tag, and is your brand new data type, like int, double, char.name, street, city, state, zipcode, age, IDnum, and grade are structure members.

****Declaring Variables of Type struct****


    The most efficient method of dealing with structure variables is to define the structure globally.  This tells "the whole world", namely main and any functions in the program, that a new data type exists.  To declare a structure globally, place it BEFORE void main().  The structure variables can then be defined locally in void main..

Simple Inheritance Example ♥

#include<iostream.h>
#include<conio.h>
class shape
{
    public:
        int width,height;
        void set_width(int w)
        {
            width = w;
        }
        void set_height(int h)
        {
            height = h;
        }
};
class rectangle:public shape
{
    public:
    int get_area()
    {
        return width * height;
    }
};
void main()
{
    clrscr();
    rectangle r1;
    r1.set_width(5);
    r1.set_height(7);
    cout <<"\n\tTotal area: " << r1.get_area();
    getch();
}

Here is the output :)


Program to display date ♥

#include<iostream.h>
#include<conio.h>
class member
{
    public:
        int day(int d)
        {
            return d;
        }
        int month(int m)
        {
            return m;
        }
        int year(int y)
        {
            return y;
        }
};
void main()
{
    clrscr();
    member m1;
    cout<<"\n\t"<<m1.day(26)<<":"<<m1.month(7)<<":"<<m1.year(2013)<<endl;
    getch();
}

Here is the Output :)

Program to display Current Time Information

#include<iostream.h>
#include<math.h>
#include<time.h>
#include<conio.h>
void main()
{
    time_t p =time(0);
    tm* time=localtime(&p);
    clrscr();

    cout<<"Seconds= " << (time->tm_sec) <<endl;
    cout<<"Minutes = " << (time->tm_min) <<endl;
    cout<<"Hours = " << (time->tm_hour) <<endl;
    cout<<"Day of month = " << (time->tm_mday) <<endl;
    cout<<"Month of year = " << (time->tm_mon)+1 <<endl;
    cout<<"Year = " << (time->tm_year)+1900 <<endl;
    cout<<"Weekday = " << (time->tm_wday )<<endl;
    cout<<"Day of year = " << (time->tm_yday )<<endl<<endl<<endl;
    cout<<"Date     " <<(time->tm_mday)<<"/"<< (time->tm_mon)+1 <<"/"<< (time->tm_year)+1900<<endl;
    cout<<"Time     " << (time->tm_hour)<<":"<< (time->tm_min)<<":"<< (time->tm_sec) <<endl;
getch();
}

Here is the output:)


Wednesday, 24 July 2013

Program To Enter Bank Details in implement

#include<iostream.h>
#include<conio.h>
#include<process.h>
class member
{
    protected:
        int dp,wd,bal,amt;
    public:
        void putdata();
        int amount();
        int deposite();
        int withdraw();
        int balance();
};
void member::putdata()
{
    cout<<"\n Enter The Amount : ";
    cin>>amt;
    cout<<"\n Enter The Deposite Amount: ";
    cin>>dp;
    cout<<"\n Enter The Withdraw Amount: ";
    cin>>wd;
}
int member::amount()
{
    return amt;
}
int member::deposite()
{
    return dp;
}
int member::withdraw()
{
    return wd;
}
int member::balance()
{
    return amt+dp-wd;
}
void main()
{
    int ch,s;
    clrscr();
    member m;
    m.putdata();
    cout<<"\n 1.Amount: \n 2.Deposite: \n 3.Withdraw: \n 4.Balance: \n 5.Exit:";
    mahi:
    cout<<"\n\n Enter The Choice: ";
    cin>>ch;
    switch(ch)
    {
        case 1:
            s=m.amount();
            cout<<"\n Amount= "<<s;
            goto mahi;
        case 2:
            s=m.deposite();
            cout<<"\n Deposite= "<<s;
            goto mahi;
        case 3:
            s=m.withdraw();
            cout<<"\n Withdraw= "<<s;
            goto mahi;
        case 4:
            s=m.balance();
            cout<<"\n Balance= "<<s;
            goto mahi;
        case 5:
            exit(0);
        default:
            cout<<"\n Abe Dekhke Number Choose Karna \n";
            goto mahi;
    }
getch();
}


Here Is The Output :)


Sunday, 21 July 2013

Write a program using three or more class ♥

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class per
{
    private:
        char name[20];
        int age;
    public:
        per(char n[20],int a)
        {
            strcpy(name,n);
            age=a;
        }
        void display()
        {
            cout<<"\n Name Is -->>: "<<setw(15)<<setfill('*')<<name;
            cout<<"\n Age Is -->>: "<<setw(16)<<setfill('*')<<age;
        }
};
class student:public per
{
    private:
        float perc;
    public:
        student(char n[20],int a,float p):per(n,a)
        {
            perc=p;
        }
        void display()
        {
            cout<<"\n Student";
            per::display();
            cout<<"\n Percentage Is-->>: "<<setw(10)<<setfill('*')<<perc;
        }
};
class techer:public per
{
    private:
        float salary;
    public:
    techer (char n[20],int a,float s):per(n,a)
    {
        salary=s;
    }
    void display()
    {
        cout<<"\n Techer ";
        per::display();
        cout<<"\n Salary Is -->>: "<<setw(13)<<setfill('*')<<salary;
    }
};
void main()
{
    clrscr();
    student s("mahi",22,99);
    techer t("devershi",21,5000);
    s. display();
    t. display();
    getch();

}

Here is the output :))


Saturday, 20 July 2013

Program to show the Multiple use of Matrix :)

#include<iostream.h> 
#include<conio.h> 
#include<process.h> 
int sum1(int a[50][50],int r) 

    int s=0;
    for(int i=0;i<r;i++)
    {
        s=s+a[i][i];
    }
    return(s);
}
int sum2(int a[50][50],int r)
{
    int s=0;
    int j;
    j=r-1;
    for(int i=0;i<r;i++)
        s=s+a[i][j--];
        return(s);
}
void row(int a[50][50],int r)
{
    int s=0;
    for(int i=0;i<r;i++)
    {
        s=0;
        for(int j=0;j<r;j++)
        {
            s=s+a[i][j];
        }
        cout<<"Sum of Row "<<i+1<<" = "<<s<<endl;
    }
}
void col(int a[50][50],int r)
{
    int s=0;
    for(int i=0;i<r;i++)
    {
        s=0;
        for(int j=0;j<r;j++)
        {
            s=s+a[j][i];
        }
        cout<<"Sum of Column "<<i+1<<" = "<<s<<endl;
    }
}
void main()
{
    clrscr();
    int i,s,j,r,c,ch,a[50][50];
    x:
    cout<<"Entr Array Limit--(Enter only Row as R=C)"<<endl;
    cin>>r;
    cout<<"Enter Array"<<endl;
    for(i=0;i<r;i++)
    {
        for(j=0;j<r;j++)
        {
            cin>>a[i][j];
        }
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<r;j++)
        {
            cout<<"\t"<<a[i][j];
        }
        cout<<"\n";
    }
    y:
    cout<<"\nEnter Choice :"<<endl<<"Sum of---- \n\t1:main \n\t2:Secondary \n\t3.Rows \n\t4.Columns \n\t5.Re-enter \n\t6.Exit "<<endl;
    cin>>ch;
    switch(ch)
    {
        case 1:
            s=sum1(a,r);
            cout<<"Sum = "<<s<<endl;
            goto y;
        case 2 :
            s=sum2(a,r);
            cout<<"Sum = "<<s<<endl;
            goto y;
        case 3:
            row(a,r);
            goto y;
        case 4:
            col(a,r);
            goto y;
        case 5:
            goto x;
        case 6:
            exit(0);
        default :
            cout<<"Wrong Choice"<<endl;
            break;
    }
getch();
}

Here is the Output :)

 

Friday, 19 July 2013

Find Grade of Student using Switch Case

#include<iostream.h>
#include<conio.h>
class grade
{
    int r,P,t,i,total,m[5];
    char a[15];
    float per;
    public:
        void Mahi();
};
void grade::Mahi()
{
    total=0;
    cout<<"\n Enter the name of the student: ";
    cin>>a;
    cout<<"\n Enter the roll number of the student: ";
    cin>>r;
    cout<<"\n Enter marks obtained in five subjects: \n\n";
    for(i=0;i<5;i++)
    {
        cout<<" Enter marks in "<<i+1<<" subject: ";
        cin>>t;
        if(t<=100)
            m[i]=t;
        else
        {
            cout<<"Marks should not exceed 100\n";
            i=i-1;
        }
    }
    for(i=0;i<5;i++)
    total=total+m[i];
    per=(total/500.0)*100;
    cout<<"\nNAME\tROLLNO\tTOTAL\tPERCENTAGE\tGRADE\n\n" ;
    cout<<a<<"\t"<<r<<"\t"<<total<<"\t"<<per<<"\t";
    P=per/10;

    switch(P)
    {
        case 10:
        case 9:
        case 8:
        case 7:
            cout<<"Grade A";
            break;
        case 6:
            cout<<"Grade B";
            break;
        case 5:
            cout<<"Grade C";
            break;
        case 4:
            cout<<"Grade D";
            break;
        default:
            cout<<"Grade Fail";
    }
}
void main()
{
    clrscr();
    grade d1;
    d1.Mahi();
    getch();
}

Here is the output :)

 

Thursday, 18 July 2013

Virtual Class Base Examples :)

1) Simple Example of virtual class 

 #include<fstream.h>

#include<conio.h>

class base

{

    public:

    virtual void show()=0;

};


class derived1 : public base

{

    public:

    void show()

    {

        cout<<"\n Mahi 1";

    }

};


class derived2 : public base

{

    public:

    void show()

    {

        cout<<"\n Mahi 2";

    }

};


void main()

{

    clrscr();

    base *b;

    derived1 d1;

    derived2 d2;

    b = &d1;

    b->show();

    b = &d2;

    b->show();

    getch();

}

Here is the output :)

 2) Virtual class example of student details

#include<conio.h>
#include<iostream.h>
class base
{
    public:

    virtual void input()=0;

    virtual void output()=0;
};
class derived:public base
{
    char name[15];
    int roll_no,age;

    public:

        void input()

        {

            cout<<"Enter name of the student: ";
            cin>>name;

            cout<<"Enter roll no of the student: ";
            cin>>roll_no;

            cout<<"Enter age of the student: ";
            cin>>age;
        }

        void output()
        {

            cout<<"Name: "<<name<<endl;

            cout<<"Roll no: "<<roll_no<<endl;

            cout<<"Age: "<<age<<endl;

        }
};
void main()
{
    base *ptr;
    derived d1;
    clrscr();
    ptr=&d1;
    ptr->input();
    cout<<"Data of student is: "<<endl;
    ptr->output();
    getch();
}

Here is the output :)

  

Wednesday, 17 July 2013

Arrange the 10 Numbers into Ascending and Descending order :)

#include<iostream.h>
#include<conio.h>
void main()
{
    int i,j,k,a[10];
    clrscr();
    cout<<"\n Enter 10 numbers: \n";
    for(i=0;i<10;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<10;i++)
    {
        for(j=0;j<10;j++)
        {
            if(a[i]>a[j])
            {
                k=a[i];
                a[i]=a[j];
                a[j]=k;
            }
        }
    }
    cout<<"\n Descending order\n";
    for(i=0;i<10;i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<"\n Ascending Order\n";
    for(i=9;i!=-1;i--)
    {
        cout<<a[i]<<" ";
    }
    getch();
}

Here is the Output :))

 

Yes No Example :P

Program to display a menu with option add, subtract, multiplication and divide.

#include<iostream.h>
#include<conio.h>
void main()
{
    int choice;
    char ch='y';
    int n1,n2;
    clrscr();
    while(ch=='y')
    {
        cout<<"\n Enter the first number: ";
        cin>>n1;
        cout<<"\n Enter the second number: ";
        cin>>n2;
        cout<<"\n 1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n";
        cout<<"\n Enter the choice: ";
        cin>>choice;

        switch(choice)
        {
            case 1:
                cout<<"\n Addition= "<<n1+n2;
                break;
            case 2:
                cout<<"\n Subtraction= "<<n1-n2;
                break;
            case 3:
                cout<<"\n Multiplication= "<<n1*n2;
                break;
            case 4:
                cout<<"\n Division= "<<n1/n2;
                break;
            default:
                cout<<"\n Invalid Choice";
                break;
        }
        cout<<"\n Do u want to continue? (y/n) : ";
        cin>>ch;
    }
getch();
}

Here is The Output :))


Finding The Greater Number From The Given 3 Numbers :))

1)

#include<iostream.h>
#include<conio.h>
void main()
{
    int a,b,c,great;
    clrscr();
    cout<<"\n Enter the First Number: ";
    cin>>a;
    cout<<"\n Enter the second number: ";
    cin>>b;
    cout<<"\n Enter the Third Number: ";
    cin>>c;
    if((a>b) && (a>c))
        great=a;
    else if((b>a) && (b>c))
        great=b;
    else
        great=c;
    cout<<"\n The great Number Is : "<<great;
    getch();
}

2)

#include<iostream.h>
#include<conio.h>
void main()
{
    int a,b,c;
    clrscr();
    cout<<"\n Enter the Values: ";
    cin>>a>>b>>c;
    if(a>b)
    {
        if(a>c)
            cout<<a<<" is great";
        else
            cout<<c<<" is great";
    }
    else if(b>c)
        cout<<b<<" is great";
    else
        cout<<c<<" is great";

getch();
}

Here is the Output :))

 

Program to Write a Member Function :)

#include<iostream.h>
#include<conio.h>
class add
{
    protected:
        int m,d;
    public:
        mahi()
        {
            m=0;
            d=0;
        }
        read()
        {
            cout<<"\n Enter a: ";
            cin>>m;
            cout<<"\n Enter b: ";
            cin>>d;
        }
        showadd()
        {
            cout<<"\n Addition= "<<m+d;
        }
};
void main()
{
    clrscr();
    add p;
    p.mahi();
    p.read();
    p.showadd();
    getch();
}

Here is the Output :))


Program to find the average of n numbers using class :)

1)

#include<iostream.h>
#include<conio.h>
class no
{
    private:
        int n,i;
        int a[10],v;
    public:
        void read()
        {
            cout<<"\n Enter the elemnts: ";
            cin>>n;
            cout<<"\n Enter "<<n<<" elements below\n";
            for(i=0;i<n;i++)
            {
                cin>>a[i];
            }
        }
        void avg()
        {
            float sum=0;
            for(i=0;i<n;i++)
            {
                sum+=a[i];
            }
            v=sum/n;
            cout<<"\n Average= "<<v;
        }
};
void main()
{
    clrscr();
    no n1;
    n1.read();
    n1.avg();
    getch();
}

2)

#include<iostream.h>
#include<conio.h>
class no
{
    private:
        int n,i;
        int a[10];
    public:
        void read()
        {
            cout<<"\n Enter the elemnts: ";
            cin>>n;
            cout<<"\n Enter "<<n<<" elements below\n";
            for(i=0;i<n;i++)
            {
                cin>>a[i];
            }
        }
        float avg()
        {
            float sum=0;
            for(i=0;i<n;i++)
            {
                sum+=a[i];
            }
            return sum/n;
        }
};
void main()
{
    clrscr();
    no n1;
    n1.read();
    cout<<"\n Average= "<<n1.avg();
    getch();
}


Write a program of switch case of day :)))

1)

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
    int ch;
    clrscr();
    while(1)
    {
        cout<<"\n\n Enter the Number (between 1 to 8): \n ";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\n Sunday";
                break;
            case 2:
                cout<<"\n Monday";
                break;
            case 3:
                cout<<"\n Tuesday";
                break;
            case 4:
                cout<<"\n Wednesday";
                break;
            case 5:
                cout<<"\n Thursday";
                break;
            case 6:
                cout<<"\n Friday";
                break;
            case 7:
                cout<<"\n Saturday";
                break;
            default:
                   exit(0);
        }
    }
getch();
}

2)

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
    int ch;
    clrscr();
    D:
    {
        cout<<"\n\n Enter the Number (between 1 to 8): \n ";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\n Sunday";
                goto D;
            case 2:
                cout<<"\n Monday";
                goto D;
            case 3:
                cout<<"\n Tuesday";
                goto D;
            case 4:
                cout<<"\n Wednesday";
                goto D;
            case 5:
                cout<<"\n Thursday";
                goto D;
            case 6:
                cout<<"\n Friday";
                goto D;
            case 7:
                cout<<"\n Saturday";
                goto D;
            default:
                   exit(0);
        }
    }
getch();
}

Here is the output :))

 

Tuesday, 16 July 2013

Write a program to add two matrix...

#include<iostream.h>
#include<conio.h>
void main()
{
    int a[3][3],b[3][3],t[3][3],i,j,r,c;
    clrscr();
    cout<<"\n Enter rows: ";
    cin>>r;
    cout<<"\n Enter columns: ";
    cin>>c;
    cout<<"\n For Matrix A\n";
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            cout<<" Enter Elements: "<<i+1<<"\t"<<j+1<<" "<<":: ";
            cin>>a[i][j];
        }
    }
    cout<<"\n For matrix B\n";
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            cout<<" Enter elements: "<<i+1<<"\t"<<j+1<<" "<<":: ";
            cin>>b[i][j];
        }
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            t[i][j]=a[i][j]+b[i][j];
        }
    }
    cout<<"\n Result Matrix is: \n";
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            cout<<"\t"<<t[i][j];
        }
        cout<<"\n";
    }
getch();
}

Here is the output :)))

  2} with class :))

#include<iostream.h>
#include<conio.h>
class matrix
{
    public:
        int a[3][3],b[3][3],t[3][3],i,j,r,c;
        void rc()
        {
            cout<<"\n Enter The Number of Rows: ";
            cin>>r;
            cout<<"\n Enter The Number of Columns: ";
            cin>>c;
        }
        void loop1()
        {
            cout<<"\n For Matrix A:";
            for(i=0;i<r;i++)
            {
                for(j=0;j<c;j++)
                {
                    cout<<"\n Enter Elements: "<<i+1<<" "<<j+1<<" "<<": ";
                    cin>>a[i][j];
                }
            }
        }
        void loop2()
        {
            cout<<"\n For Matrix B: ";
            for(i=0;i<r;i++)
            {
                for(j=0;j<c;j++)
                {
                    cout<<"\n Enter Elements: "<<i+1<<" "<<j+1<<" "<<": ";
                    cin>>b[i][j];
                }
            }
        }
        void sum()
        {
            for(i=0;i<r;i++)
            {
                for(j=0;j<c;j++)
                {
                    t[i][j]=a[i][j]+b[i][j];
                }
            }
        }
        void total()
        {
            cout<<"\n For Result Matrix: \n";
            for(i=0;i<r;i++)
            {
                for(j=0;j<c;j++)
                {
                    cout<<"\t"<<t[i][j];
                }
                cout<<"\n";
            }
        }
};
void main()
{
    clrscr();
    matrix m1;
    m1.rc();
    m1.loop1();
    m1.loop2();
    m1.sum();
    m1.total();
    getch();
}

Here is the output :)))

 

Monday, 15 July 2013

SIMPLE EXAMPLE OF PROCESS HEADER FILE

Write a simple program of addition,multiplication,subtraction and division using process header file :)))

 

#include<iostream.h>
#include<conio.h>
#include<process.h>
int add(int a,int b)
{
    int sum;
    sum=a+b;
    return sum;
}
int sub(int a,int b)
{
    int sum;
    sum=a-b;
    return sum;
}
int mul(int a,int b)
{
    int sum;
    sum=a*b;
    return sum;
}
int div(int a,int b)
{
    int sum;
    sum=a/b;
    return sum;
}
void main()
{
    int m,n,sum,ch;
    clrscr();
    cout<<"\n 1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Exit";
    P:
    cout<<"\n Enter the choice: ";
    cin>>ch;
    switch(ch)
    {
        case 1:
        cout<<"\n Enter M: ";
        cin>>m;
        cout<<"\n Enter N: ";
        cin>>n;
        sum=add(m,n);
        cout<<"\n Addition= "<<sum;
        goto P;

        case 2:
        cout<<"\n Enter M: ";
        cin>>m;
        cout<<"\n Enter N: ";
        cin>>n;
        sum=sub(m,n);
        cout<<"\n Subtraction= "<<sum;
        goto P;

        case 3:
        cout<<"\n Enter M: ";
        cin>>m;
        cout<<"\n Enter N: ";
        cin>>n;
        sum=mul(m,n);
        cout<<"\n Multiplication= "<<sum;
        goto P;

        case 4:
        cout<<"\n Enter M: ";
        cin>>m;
        cout<<"\n Enter N: ";
        cin>>n;
        sum=div(m,n);
        cout<<"\n Division= "<<sum;
        goto P;

        case 5:
        exit(0);
    }
getch();
}

Here is the output :)))

 

Examples of Strings and Functions

Program of swap using call by reference:)

#include<iostream.h>
#include<conio.h>
void swap(int &m, int &d)
{
    int md;
    md=m;
    m=d;
    d=md;
}
void main(void)
{
    int a=10;
    int b=20;
    clrscr();
    cout<<"\n Before swapping"<<endl;
    cout<<"\n A= "<<a<<" and B= "<<b<<endl<<endl;
    swap(a,b);
    cout<<"\n After Swapping"<<endl;
    cout<<"\n A= "<<a<<" and b= "<<b;
    getch();
}

Here is the output :)))

2) Print total vowels from the string --->

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
    int vowel=0;
    int k=0;
    char r[50];
    clrscr();
    cout<<"\n Enter the string: ";
    cin>>r;
    while(k<50 && r[k]!='\0')
    {
        switch(r[k])
        {
            case 'a':
            case 'A':
            case 'e':
            case 'E':
            case 'i':
            case 'I':
            case 'o':
            case 'O':
            case 'u':
            case 'U':
                vowel++;
                break;
        }
        k++;
    }
    cout<<"\n There are "<<vowel<<" vowels in the string";
getch();
}

Here is the output :)))

3) Program to print the square of given number using function:))

#include<iostream.h>
#include<conio.h>
void main()
{
    int sq(int);
    int a,ans;
    clrscr();
    cout<<"\n Enter the value of a: ";
    cin>>a;
    ans=sq(a);
    cout<<"\n Square= "<<ans;
    getch();
}
int sq(int d)
{
    int ans;
    ans=d*d;
    return ans;
}

Here is the output :)))

 

 

 

4) Write the program to find the simple interest using function:))

#include<iostream.h>
#include<conio.h>
void main()
{
    double si(double,double,double);
    double amt,r,n,ans;
    clrscr();
    cout<<"\n Enter the Amount: ";
    cin>>amt;
    cout<<"\n Enter the Interest Rate: ";
    cin>>r;
    cout<<"\n Enter the no of years: ";
    cin>>n;
    ans=si(amt,r,n);
    cout<<"\n Simple Instrest = "<<ans;
    getch();
}
double si(double a,double b,double c)
{
    double ans;
    ans=(a*b*c)/100;
    return ans;
}

 

Here is the output :)))

 

5) Write the program to count the vowels and consonant from the given string:)))

#include<iostream.h>
#include<conio.h>
void main()
{
    char s[50];
    int d=0;
    int vowel=0;
    int consonant=0;
    clrscr();
    cout<<"\n Enter the string: ";
    cin>>s;
    while(d<50 && s[d]!='\0')
    {
        switch(s[d])
        {
            case 'a':
            case 'A':
            case 'e':
            case 'E':
            case 'i':
            case 'I':
            case 'o':
            case 'O':
            case 'u':
            case 'U':
                vowel++;
                break;
            default:
                consonant++;
                break;
        }
        d++;
    }
    cout<<"\n There are "<<vowel<<" vowels ";
    cout<<"\n and "<<consonant<<" consonants";
getch();
}

Here is the output :)))