Sunday, 21 December 2014

Factorial

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a;
cout<<"Enter any number:";
cin>>a;
i=a-1;
while(i>0)
{
a=a*i;
i--;
}
cout<<a;
getch();
}

Diamond Pattern

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,l,m,n;
for(i=1;i<=5;i++)
{
for(j=5-i;j>=1;j--)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"# ";
cout<<endl;
}
for(l=4;l>=1;l--)
{
for(m=5-l;m>=1;m--)
cout<<" ";
for(n=1;n<=l;n++)
cout<<"# ";
cout<<endl;
}
getch();
}

Calculator

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d,e,f,g;
cout<<"Enter two number:";
cin>>a>>b;
cout<<"Enter your choice:";
cin>>c;
switch(c)
{
case 1:
d=a+b;
cout<<d;
break;
case 2:
e=a-b;
cout<<e;
break;
case 3:
f=a*b;
cout<<f;
break;
case 4:
g=a/b;
cout<<g;
break;
default:
cout<<"Invalid Operator";
}
getch();
}

Pattern

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
char k='A';
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
cout<<char(k);
cout<<endl;
k++;
}
getch();
}

Friday, 19 December 2014

Q.Create a class student with data members name,class,section,roll no. and function
members getdata(),printdata(),and promoted().From this class derive a class 'Sr_std'
with additional data member stream.Also include another function member change_stream().

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
char ch[5];
class Student
{
    private:
    char name[30];
    int cla;
    char section[2];
    int roll_no;
    public:
    Student()
    {
     strcpy(name,"ABCD");
     cla=1;
     strcpy(section,"A");
     roll_no=1;
    }
    void getdata()
    {
     cout<<"Enter name:";
     gets(name);
     cout<<endl<<"Enter class:";
     cin>>cla;
     cout<<"Enter section:";
     cin>>section;
     cout<<"Enter roll number:";
     cin>>roll_no;
    }
    void print()
    {
     cout<<endl<<"Name:";
     puts(name);
     cout<<endl<<"Class:";
     cout<<cla;
     cout<<endl<<"Section:";
     cout<<section;
     cout<<endl<<"Roll number:";
     cout<<roll_no;
    }
    protected:
    void promote()
    {
     cout<<"Congratulation!!! Promoted to:"<<cla+1;
     cla=cla+1;
    }
};
class Sr_std:public Student
{
    private:
    char stream[5];
    public:
    Sr_std()
    {
     strcpy(stream,"PCM");
    }
    void getdata()
    {
     Student::getdata();
     cout<<"Enter stream:";
     gets(stream);
    }
    void print()
    {
     Student::print();
     cout<<endl<<"Stream:"<<stream;
    }
    void change_stream()
    {
     cout<<"Enter desired stream:";
     gets(ch);
     strcpy(stream,ch);
    }
    void promote()
    {
     Student::promote();
    }
};
void main()
{
    clrscr();
    char str;
    Sr_std A;
    A.getdata();

    cout<<endl<<"Want to change stream:(Y/N)";
    cin>>ch;
    if(strcmp(ch,"Y")==0)
    {
    A.change_stream();
    A.print();
    }
    else
    A.print();
    int marks;
    cout<<endl<<"Enter % marks obtained:";
    cin>>marks;
    if(marks>40)
    A.promote();
    else
    cout<<"Fail!!!!";
    getch();
}
 Q.WAP using the above class to define an array of 10 points and input
the data in the array.Then for each point tell if it lies on any axis
(x-axis or y-axis).

#include<iostream.h>
#include<conio.h>
#include<math.h>
int i;
class Point
{
     private:
     int x,y;
     public:
     Point()
     {
    x=0;
    y=0;
     }
     void getinfo()
     {
    cout<<"Enter co-ordinates of the point:";
    cin>>x>>y;
     }
     void print()
     {
    cout<<"("<<x<<","<<y<<")";
     }
     int return_X()
     {
    return x;
     }
     int return_Y()
     {
    return y;
     }
};
void main()
{
     clrscr();
     Point P[10];
     for(i=0;i<10;i++)
     {
      P[i].getinfo();
     }
     for(i=0;i<10;i++)
     {
    if(P[i].return_Y()==0)
    {
       cout<<endl<<"Point:";
       P[i].print();
       cout<<endl<<"Lie on x-axis.";
    }
    if(P[i].return_X()==0)
    {
       cout<<endl<<"Point:";
       P[i].print();
       cout<<endl<<"Lie on y-axis.";
    }
    if(P[i].return_X()==0 && P[i].return_Y()==0)
    {
       cout<<endl<<"Origin";
    }
     }
     getch();
}
Q.Define a point class for 2-D points(x,y).Include a default constructor , a copy constructor ,
a negate() function to transform the point into its negative , a norm() function to return the
point's distance from the point's distance from the origin(0,0),and a print() function besides
the functions to input and display the co-ordinates of the point.

#include<iostream.h>
#include<conio.h>
#include<math.h>
class Point
{
     private:
     int x,y;
     public:
     Point()
     {
    x=0;
    y=0;
     }
     Point(Point&Q)
     {
    x=Q.x;
    y=Q.y;
     }
     void negate()
     {
    x=-1*x;
    y=-1*y;
     }
     float norm()
     {
    float num;
    num=sqrt(x*x+y*y);
    return num;
     }
     void getinfo()
     {
    cout<<"Enter co-ordinates of the point:";
    cin>>x>>y;
     }
     void print()
     {
    cout<<"("<<x<<","<<y<<")";
     }
};
void main()
{
     clrscr();
     Point P;
     P.getinfo();
     Point(P);
     cout<<endl<<"Co-ordinate of the point:";
     P.print();
     float t=P.norm();
     cout<<endl<<"Distance from the origin:"<<t;
     P.negate();
     cout<<endl<<"The negative of the co-ordinates:";
     P.print();
     getch();
}