You are on page 1of 43

/Define a class Right angled Triangle, and determine its area and perimeter. #include<iostream> #include<math.

h> using namespace std; class RightTriangle { private : double s1,s2,s3,s ; double area ; double perimeter; public : RightTriangle() { s1,s2 =0.00; area=0.00; perimeter=0.00; } void input(); double area1(); double perimeter1(); void output(); };

void RightTriangle :: input() { cout<<"\n\n ENTER THE SIDES OF THE TRIANGLE:\n\n"; cout<<"\n\n BASE :\t"; cin>>s1; cout<<"\n\n PERPENDICULAR :\t";

cin>>s2; s3=sqrt((s1*s1+s2*s2)); }

double RightTriangle::perimeter1() { return(s1+s2+s3); };

double RightTriangle::area1() { double m; s=(s1+s2+s3)/2; m=sqrt(s*(s-s1)*(s-s2)*(s-s3)); return m; }

void RightTriangle::output() { cout<<"\n\n The Area of the Right Angled Triangle :\t"; cout<<area1(); cout<<"\n\n The Perimeter of the Right Angled Triangle :\t"; cout<<perimeter1(); cout<<"\n\n\n\n"; }

int main() { cout<<"\n\n\n ***PROGRAM TO EVALUATE AREA,PERIMETER OR RIGHT-ANGLED TRIANGLE**\n\n";

char ch; do{ RightTriangle r1; r1.input(); r1.output(); cout<<" \n\n\n Holding the Output \n\n\n\n"; cout<<"\n\n AGAIN(Y/N) ...? :\t"; cin>>ch; }while(ch=='y'||ch=='Y'); return 0; } OUTPUT:

/* Define a class, complex no that has the behaviour add subtract. Use this class in the main program to define two objects. Output the sum and diff of these two objects. */

#include<iostream> #include<conio.h> using namespace std;

class complex { private: double real,imag; public : complex() { real=0.00; imag=0.00; }; void input(); void addition(complex,complex); void difference(complex,complex); void display(); void mult(complex,complex);

~complex() { }; }; void complex::input()

{ cout<<"\n Real Part :\t"; cin>>real; cout<<"\n Imaginary Part:\t"; cin>>imag; }

void complex::addition(complex c1,complex c2) { real = c1.real+c2.real; imag = c1.imag+c2.imag; }

void complex::difference(complex c1,complex c2) { real = c1.real-c2.real; imag = c1.imag-c2.imag; } void complex::mult(complex c1,complex c2) { real=c1.real*c2.real-c1.imag*c2.imag; imag=c1.real*c2.imag+c2.real*c1.imag; }

void complex::display() { cout<<" Complex_Number:\t"<<real<<" + i("<<imag<<")"; }

int main() { complex cn1,cn2,cn3,cn4,cn5;

cout<<"\n

**COMPLEX NUMBERS ADDITION AND SUBSTRACTION MENU**\n";

cout<<"\nCOMPLEX_1 :\t";

cn1.input(); cout<<"\nCOMPLEX_2 :\t";

cn2.input(); cout<<"\n SUM:\t\t";

cn3.addition(cn1,cn2); cn3.display(); cout<<"\n DIFFERENCE:\t";

cn4.difference(cn1,cn2); cn4.display(); cout<<"\n\n PRODUCT:\t\t";

cn5.mult(cn1,cn2); cn5.display(); return 0; } OUTPUT:

/*Define a Class INCOME having interface pay salary, compute deductions, calculate tax. Salary consists of : 1.Basic 2.DA : Dearness Allowance 3.HRA ( 15% of the BASIC PAY) :Health Reimbursement Account 4.Deductions for Canteen/PF (8% of the BASIC PAY) 5.Tax is 20% for annual income less then one lakh rupee and 30% above this.

-Computing the INCOME status. Allow the User to enter a rate for the DA.

*/

#include<iostream> #include<conio.h> #include<process.h> #include<stdio.h> #include<dos.h>

using namespace std; /* CLASS INCOME */ class income { private : int empid; double basic; public: int rate;

double DA,HRA; double deduc; double total;

income() { empid =0; basic=0.00; } void input() { cout<<"**ENTER EMPLOYEE DETAILS:\n"; cout<<"\n EMPLOYEE_ID:\t"; cin>>empid; cout<<"\n BASIC_PAY(Monthly):\t"; cin>>basic; cout<<"\n DA_Rate:\t"; cin>>rate; } void compute() {

DA=(basic*rate)/100; HRA=(basic*rate)/100; deduc=(basic*8)/100; total=basic+DA+HRA-deduc;

} double tax; void computetax()

{ total=total*12; if(total<100000) { cout<<"\n TAX:20%\n"; tax=(total*20)/100; } else { cout<<"\n TAX:30%\n"; tax=(total*30)/100; } } void output() { compute(); cout<<"\n\n\n\n **FOR THE EMPLOYEE**\n\n\n"; computetax();

cout<<"\n EMPLOYEE_ID:\t"<<empid<<endl; cout<<"\n BASIC_PAY ( Monthly ):\t"<<basic<<endl; cout<<"\n DA:\t"<<DA<<endl; cout<<"\n HRA:\t"<<HRA<<endl; cout<<"\n TOTAL DEDUCTIONs:\t"<<deduc<<endl; cout<<"\n ANNUAL TOTAL:\t"<<total<<endl; cout<<"\n TOTAL TAX:\t"<<tax<<endl; cout<<"\n AFTER_TAX_INCOME:\t"<<(total-tax)<<endl; } };

int main() { income ob; ob.input(); ob.output(); cout<<"\n\n"; return 0; } OUTPUT:

//Program which takes entries of Employee, and updates Rank or Increments Basic Pay #include<iostream.h> #include<conio.h> #include<process.h>

class employee { long int number; int DOB[8]; long double salary; unsigned int rank;

public: employee(); void input(); void output(); void increment() { salary+=salary*10; cout<<"\n\t\tSalary incremented by 10% !"<<(char)1; } void promote() { if(rank==1||rank==0) cout<<"Employee Already on the heighest post or invalid data in Database!"; else { cout<<"\n\t\tEmployee promoted to rank "<<--rank; salary+=salary*0.25; cout<<"\n\t\tSalary = "<<salary;

} } }; employee::employee() { number=0; for(int i=0;i<8;i++) DOB[i]=0; salary= 0; rank=0; }

void employee::input() { cout<<"\n\t\tEnter the Employee number : "; cin>>number; cout<<"\n\t\tEnter the Employee salary : "; cin>>salary; cout<<"\n\t\tEnter the Employee Rank : "; cin>>rank; cout<<"\n\t\tEnter the Employee DOB [dd/mm/yyyy]: "; for(int i=0;i<8;i++) { if(i==2||i==4) cout<<':'; DOB[i]=((int)getche())-'0'; } }

void employee::output() { cout<<"\n\n\t\tEmployee Number = "<<number; cout<<"\n\t\tEmployee Salary = "<<salary;

cout<<"\n\t\tEmployee Rank = "<<rank; cout<<"\n\t\tEmployee DOB = "; for(int i=0;i<8;i++) { if(i==2||i==4) cout<<':'; cout<<DOB[i]; } }

int main() { int c; employee e; while(1) {

cout<<"\n\n\t\t\tMENU"; cout<<"\n1. Input Employee Info "; cout<<"\n2. Output Employee Info "; cout<<"\n3. Increment Salary(10%) "; cout<<"\n4. Promote Employee Rank "; cout<<"\n5. Exit"; cout<<"\nEnter your choice : "; cin>>c; if(c==1) { e.input(); } if(c==2)

{ e.output(); } if(c==3) { e.increment(); } if(c==4) { e.promote(); } if(c==5) exit(1); getch(); } return 0; } OUTPUT:

// **LINKED_LIST** /*Write a program to create a linked list and perform insertion and deletion (at start, end and at any position in the list) using self referential class. */

#include<iostream> #include<conio.h> #include<string.h> using namespace std; /* SELF REFRENTIAL CLASS TO IMPLEMENT A LINKED_LIST */

class List { protected : struct node { int info ; struct node *next ; }; typedef struct node* NODEPTR; NODEPTR listptr ; public :

/* CONSTRUCTOR*/ List() { listptr = 0; } ~List(); int emptylist();

void insafter(int oldvalue , int newvalue); void push(int newvalue); void del(int oldvalue); int pop(); void display(); };

/* DESTRUCTOR*/ List::~List() { NODEPTR p, q ;

for(p=listptr , q=p->next;p!=0; p=q,q=p->next) delete p ; }

int List::emptylist() { return(listptr==0); }

/* INSERTING AFTER A PARTICULAR ELEMENT */ void List::insafter(int oldvalue , int newvalue) { NODEPTR p,q ; for(p=listptr ; p!=0 && p->info != oldvalue ; p = p->next) ; if(p==0) printf("ERROR: value sought is not on the list . "); q = new node ;

q->info = newvalue ; q->next = p->next; p->next = q; } /* PUSHING A NEW ELEMENT IN AN LIST */ void List::push(int newvalue) { NODEPTR p ; p = new node ; p->info = newvalue ; p->next = listptr ; listptr=p; } /* DELETEING A PARTICULAR ELEMENT FROM THE LIST */ void List::del(int oldvalue) { NODEPTR p,q ; for(q=0,p=listptr; p!=0 && p->info!=oldvalue;q=p,p=p->next) ; if(p == 0) printf("ERROR: value sought is not on the list. "); if(q == 0) listptr=p->next; else q->next=p->next; delete p; } /* POPPING ELEMENTS FROM THE LINKED LIST */ int List::pop() { NODEPTR p ; int x; if(emptylist())

printf("ERROR: the list is empty ."); p= listptr; listptr = p->next; x=p->info; delete p; return x; }

/* DISPLAYING THE ELEMENTS OF THE LINKED LIST */ void List::display() { NODEPTR p; p=listptr; for(;p!=0;p=p->next) { cout<<"->"<<p->info; } } int main() { List l1; char ch; /* MENU_DRIVEN STRUCTURE TO IMPLEMENT THE LIST*/ do

{ int c,x,y; cout<<"\n **LIST MENU **\n"; cout<<"\n 1.PUSH into List\n"; cout<<"\n 2.POP from list\n";

cout<<"\n 3.Insert After\n"; cout<<"\n 4.Delete\n"; cout<<"\n 5.Display\n"; cin>>c; switch(c) { case 1:cout<<"\n ELEMENT..?:\t"; cin>>x; l1.push(x); break; case 2:cout<<"\n DELETED:\t"; cout<<l1.pop(); break; case 3:cout<<"\n INSERT after:\t"; cin>>x; cout<<"\n ELEMENT..?:\t"; cin>>y; l1.insafter(x,y); break; case 4:cout<<"\n ELEMENT to be deleted:\t"; cin>>x; l1.del(x); break; case 5:cout<<"\n DISPLAYING elements:\t"; l1.display(); break; default:cout<<"\n ENTER VALID OPTION\n"; break; } cout<<"\n MORE..?(Y/N):\t";

cin>>ch; }while(ch=='y'||ch=='Y'); cout<<"\n\n\n\n"; system("PAUSE"); return 0; } OUTPUT:

// STRING_CLASS /* Create a strt class that allows the following types of operator:

1.String Concatenation using the + operator. 2.String Assignment using the = operator. 3.String Comparison using the <,>,== operator 4.Overloading >> and << Operators to print the class details. */ #include<iostream> #include<conio.h>

using namespace std;

/* STR_CLASS*/ class str { private: char arr[90]; int len; public: str() { len=0; } str operator+(str s1); int operator>(str s1); int operator==(str s1);

friend ostream &operator<<(ostream &out, str c) ; friend istream &operator>>(istream &in, str &c); //output };

/* OVERLOADED >> OPERATORS*/ istream &operator>>(istream &in, str &c) { //input

cout<<"Enter String :\n"; cin.getline(c.arr,90); for( int i=0;c.arr[i]!='\0';i++) { c.len++; }

return in; }

/* + Operator Overloading */ str str::operator+(str s1) { str s3; int j=0; for( int i=0;arr[i]!='\0';i++) { s3.arr[j++]=arr[i]; }

s3.arr[j++]=' ';

for(int i=0;s1.arr[i]!='\0';i++) { s3.arr[j++]=s1.arr[i]; }

s3.arr[j++]='\0'; s3.len=j; return(s3); }

int str::operator ==(str s1) { int flag; if(len!=s1.len) return 0; else { for(int i=0;arr[i]!='\0';i++) { if(arr[i]==s1.arr[i]) { flag=1; continue; } else { flag=0; break; }

} if(flag==1) return 1; else return 0; } }

/* > Operator Overloading */ int str::operator>(str s1) { if(len>s1.len) return 1; else return 0; } /* << Operator Overloading */ ostream &operator<<(ostream &out, str c) { out<<"String:\t "<<c.arr<<endl; out<<"String Lenght:\t"<<c.len<<"\n"; return out; } int main() { str s1,s2,s3; cout<<"\n STRING_1 and STRING_2:\n\n\n"; cin>>s1>>s2; s3=s1+s2; cout<<"\n CONCATENATED STRING:\t"; //output

cout<<s3; if(s1==s2) cout<<"\n STRINGS are Enqual\n"; else if(s1>s2) cout<<"\n STRING_1 is greater\n"; else cout<<"\n STRING_2 is greater\n"; return 0; } OUTPUT:

/* Using the concept of polymorphism. Write a program to calculate the area and parameter of diff kind of shapes Point,Circle,Rectangles,Squares,Line */

class point { int x,y;

int area(point); int area(point, int radios); int area(point,point); int area(point,point,point); int area(point,point,point,point); }; */

// for point // for circle // for line // for triangle // for rectangles

#include<iostream> #include<conio.h> #include<stdio.h> #include<cmath>

using namespace std;

class point { public:

double x,y;

void getdata() { cout<<"\n Ordiante:\t"; cin>>x; cout<<"\n Abscissa:\t"; cin>>y; } void area(point); void area(point, int radios); void area(point,point); void area(point,point,point); void area(point,point,point,point); };

void point::area(point m) { cout<<"\n THE GIVEN IS A POINT, WHOSE AREA and PERIMETER cannot be calculate:\t"; cout<<"\n POINT:\t"; cout<<"( "<<m.x<<", "<<m.y<<" )"<<endl; }

void point::area(point m,int rad) { cout<<"\n THE GIVEN IS A CIRCLE of the type :\t"; cout<<"(x-"; cout<<m.x; cout<<")^2+"; cout<<"(y-";

cout<<m.y; cout<<")^2 = \t"; cout<<rad*rad; cout<<"\n AREA of the circle:\t"<<3.14*rad*rad<<endl; cout<<"\n PERIMETER of the circle:\t"<<2*3.14*rad<<endl; } void point::area(point m,point n) { double len; cout<<"\n THE GIVEN LINE"; cout<<"\n THE LENGTH of the line:\t"; len=sqrt(((m.x-n.x)*(m.x-n.x)) + ((m.y-n.y)*(m.y-n.y))); cout<<len; } void point::area(point m,point n,point l) { double len1,len2,len3,len,area,perimeter; cout<<"\n THE GIVEN IS A TRIANGLE :\t"; len1=sqrt(((m.x-n.x)*(m.x-n.x)) + ((m.y-n.y)*(m.y-n.y))); len2=sqrt(((m.x-l.x)*(m.x-l.x)) + ((m.y-l.y)*(m.y-l.y))); len3=sqrt(((l.x-n.x)*(l.x-n.x)) + ((l.y-n.y)*(l.y-n.y))); cout<<"\n SIDES of the triangle:\t"; cout<<len1<<" : "<<len2<<" : "<<len3<<endl; len=(len1+len2+len3)/2; area=sqrt((len-len1)*(len-len2)*(len-len3)*(len)); cout<<"\n AREA of the triangle:\t"; cout<<area<<" Sq Units "; cout<<"\n PERIMETER of the triangle:\t"<<len*2<<" Units"<<endl;

int main() { system("CLS"); int radius; point x,y,z; x.getdata(); y.getdata(); z.getdata(); cout<<"\n ENTER Radius:\t"; cin>>radius; x.area(y,z); x.area(y,radius); point b; b.getdata(); x.area(z,y,b); cout<<"\n\n\n\n"; system("PAUSE"); return 0; } OUTPUT:

/*write a program using virtual fu's, base class and derived classes and their member data are illustrated bellow:

#include<iostream> #include<conio.h> #include<string.h>

using namespace std;

class company{ char name[50]; public: virtual void input() { cout<<"\nCompany Name:\t"; gets(name); } virtual void display() { cout<<"\n Company Name:\t"; cout<<name<<endl; } };

class desktop: public company{ char mname[50]; long double price; /*For extra digit precision*/ public:

desktop() /*Constructor*/ { price=0; } void input() { cout<<"\nMODEL NAME:\t"; cin>>mname; cout<<"\nPRICE:\t"; cin>>price; } void display() { cout<<"\nMODEL:\t"; cout<<mname; cout<<"\nPRICE:\t"; cout<<price; } void disc() { if(price>=40000) { cout<<"\nYou get a 2000 discount"; cout<<"\nYour new price is"; cout<<(price-2000); } } }; class laptop: public company{

char lname[50]; long double price; /*For extra digit precision*/ public: laptop() /*Constructor*/ {price=0; } void input() { cout<<"\nLaptop Name:\t"; cin>>lname; cout<<"\nPrice:\t"; cin>>price; } void display() { cout<<"\nLAPTOP:\t"; cout<<lname; cout<<"\nPRICE:\t"; cout<<price; } void free(); }; void laptop::free() { if(price>=50000) {cout<<"\nYou get a free mouse";} }

int main() { char ch[50];

company *c,*x,*y; company s; c=&s; c->input(); c->display(); laptop l; x=&l; desktop d; y=&d; cout<<"\nLaptop/Desktop....?"; cin.getline(ch,50); if(strcmpi(ch,"laptop")==0) { x->input(); l.free(); cout<<"\n FINAL OUTPUT:\t"; x->display(); cout<<"\n\n"; } else if(strcmpi(ch,"desktop")==0) { y->input(); d.disc(); cout<<"\n FINAL OUTPUT:\t"; y->display(); cout<<"\n\n\n\n"; } else { cout<<"\nYou have entered the wrong choice--"; return 0; }

/* Create Binary TREE */ /* CREAT_BTS.C */ #include <iostream.h> # include<stdio.h> # include<malloc.h> #define SIZE 10

struct tree { struct tree *left_child; int info; struct tree *right_child; }; typedef struct tree Tnode;

Tnode *root=NULL,*ptr,*par,*New; int top=SIZE; Tnode *stack[SIZE];

void create(Tnode *); void display(Tnode *); void push(Tnode *); Tnode *pop();

int main() { char x; create(root); printf("created tree is as follow:");

display(root); return 0; }

void create(Tnode *ptr) { int x; char choice; do { New= (Tnode *) malloc(sizeof(Tnode)); printf("Enter element:->"); scanf("%d",&x); New->info=x; New->left_child=NULL; New->right_child=NULL; /* attach new node in to its correct position */

if(root==NULL) /* insert at root */ root=New; else { /* locate the leaf node */ ptr=root; while(ptr!=NULL) { if(x==ptr->info) { printf("already present"); exit(0);

} par=ptr; if(x<ptr->info) ptr=ptr->left_child; else ptr=ptr->right_child; } if(x<par->info) /* insert as left child */ par->left_child=New; else par->right_child=New; /* insert as right child */

} printf("more element:->"); cin>>choice; }while(choice=='y'); }

void display(Tnode *ptr) { int i; if (ptr!=NULL) { /* pre_order tree traversal */ printf("\n pre order traversal is:->"); while(ptr!=NULL) { printf("%d ",ptr->info); /* process the root */ if(ptr->right_child!=NULL) /* push the right sub tree on stack */ push(ptr->right_child);

if(ptr->left_child!=NULL) /* move to left subtree */ push(ptr->left_child); else ptr=pop(); } } }

void push(Tnode *x) { if(top==0) printf("stack is full"); else { top=top-1; stack[top]=x; } } Tnode *pop() { Tnode *x; if(top==SIZE) { printf("\n tree has been traversed"); exit(0); } else { x=stack[top]; top=top+1;

} return(x); } OUTPUT:

//To Calculate mathematical expression in a string // (single digits only) ,No BODMAS #include<iostream.h> #include<conio.h> #include<stdio.h> #include<ctype.h>

class ABC { char string[40]; float result; public: void read() {cout<<"\n\n\t\tEnter the Mathematical Expression : "; gets(string); } void calc(); }abc; void ABC::calc() { for(int i=0;string[i]!='\0';i++) { if(isdigit(string[i])) { result+=int(string[i])-48; } else if(string[i]=='+') { result+=((int)string[++i]-48); } else if(string[i]=='-')

{ result-=((int)string[++i]-48); } else if(string[i]=='*') { result*=((int)string[++i]-48); } else if(string[i]=='/') { result/=((int)string[++i]-48); } } cout<<" = "<<result; }

int main() { //clrscr(); abc.read(); abc.calc(); return 0; } OUTPUT: