Sunday, July 4, 2010

PROGRAM TO IMPLEMENT SPARSE MATRIX(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
class matrix
{
int a[20][20],b[20][20],i,j,x,y,m,n;
public:
void input(int,int);
void sparse();
};
void matrix::input(int x,int y)
{
m=x;
n=y;
cout<<"enter the matrix\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"the maatrix is\n";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<a[i][j];
}}
void matrix::sparse()
{
int k=1;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=0)
{
b[k][0]=i;
b[k][1]=j;
b[k][2]=a[i][j];
k++;
}}}
b[0][0]=m;
b[0][1]=n;
b[0][2]=k-1;
cout<<"\nthe sparse matrix of the given mtrix \n";
for(i=0;i<k;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
cout<<b[i][j];
}}
void main()
{
clrscr();
int m,n;
matrix ob;
cout<<"enter order\n";
cin>>m>>n;
ob.input(m,n);
ob.sparse();
getch();
}

OUTPUT
enter order
3 3
enter the matrix
0 0 1
2 0 0
0 3 1
the sparse matrix is
3 3 4
0 2 1
1 0 2
2 1 3
2 2 1

PROGRAM TO ILLUSTRATE SELECTION SORT(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
class sort
{
public:
int a[50],n,i,j;
void selectionsort();
};
void sort::selectionsort()
{
int min,temp;
cout<<"\nenter the maximum no of elements\n";
cin>>n;
cout<<"enter the elements\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{temp=a[min];
a[min]=a[i];
a[i]=temp;
}}
cout<<"array after sorting is\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void main()
{
clrscr();
sort ob;
ob.selectionsort();
getch();
}

OUTPUT:
enter the maximum no of elements
5e
Enter the elements
54
15
23
2
10
array after sorting is
2 10 15 32 54

PROGRAM TO PERFORM PATTERN MATCHING(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char a[50],b[50];
int m,n,i,j,k=1,ok;
clrscr();
cout<<"enter the first string\n";
gets(a);
cout<<"enter the second string\n";
gets(b);
n=strlen(a);
m=strlen(b);
for(i=0;i<n;i++)
{
for(j=k;j<m;j++)
{
if (b[i]==a[j])
{
ok=1;
k++;
break;
}
else
ok=0;
}}
if(ok==1)
cout<<"success\n";
else
cout<<"not\n";
getch();
}

OUTPUT:
enter the first string
farook
enter the second string
rook
success

PROGRAM TO APPEND TWO ARRAYS(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[25],b[25],c[25],i,j,m,n,x=0;
clrscr();
cout<<"Enter the first string\n";
gets(a);
cout<<"Enter the second string\n";
gets(b);
m=strlen(a);
n=strlen(b);
for(i=0;i<m;i++)
c[i]=a[i];
for(j=i;j<(m+n);j++)
{
c[j]=b[x];
x++;
}
cout<<"The appended array is \n";
for(i=0;i<(m+n);i++)
cout<<c[i];
getch();
}

OUTPUT:
Enter the first string
farook
Enter the second string
college
The appended array is
farookcollege

PROGRAM TO ILLUSTRATE LINKED LIST OPERATIONS(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
struct node
{
node*next;
int data;
};
class linkedlist
{
public:
node*start;
node*last;
node*temp;
node*head;
int c;
linkedlist()
{
start=last=NULL;
}
void insertbeg();
void insertlast();
void deletbeg();
void deletlast();
void display();
};
void linkedlist :: insertbeg()
{
head=new node;
cout<<"enter the data\n";
cin>>head->data;
if(start==NULL)
{
head->next=NULL;
start=last=head;
}
else
{
head->next=start;
start=head;
}}
void linkedlist::insertlast()
{
head=new node;
cout<<"enter the data\n";
cin>>head->data;
head->next=NULL;
if(start==NULL)
{
start=last=head;
}
else
{
last->next=head;
last=head;
}}
void linkedlist::deletbeg()
{
if(start==NULL)
cout<<"list is empty\n";
else if(start==last)
start=last=NULL;
else
{
temp=start->next;
delete start;
start=temp;
}}
void linkedlist::deletlast()
{
node*prev;
node*temp;
if(start==NULL)
cout<<"list is empty\n";
else if(start==last)
start=last=NULL;
else
{
temp=start;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
delete temp;
prev->next=NULL;
last=prev;
}}
void linkedlist:: display()
{
node*temp;
temp=start;
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}
if(start==NULL)
cout<<"empty\n";
}
void main()
{
clrscr();
linkedlist ob;
int c;
do
{
cout<<"\nMENU\n1-Insertbeginning\n2-Insertlast\n3-Deletebeginning\n4-
Deletlast\n5-Display\n6-Exit\n";
cin>>c;
switch(c)
{
case 1:ob.insertbeg();
break;
case 2:ob.insertlast();
break;
case 3:ob.deletbeg();
break;
case 4:ob.deletlast();
break;
case 5:ob.display();
break;
}}
while(c<=5);
getch();
}

OUTPUT:
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
1e
Enter the data
10
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
1e
Enter the data
20
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5
20
10
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
2e
Enter the data
30
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5
20
10
30
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
3
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5
10
30
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
4
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5
10
4
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5l
List is empty
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
6


PROGRAM TO EVALUATE POSTFIX EXPRESSION(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char a[10];
int b[10],i,n,q,c=0,r;
clrscr();
cout<<"enter the postfix expression\n";
gets(a);
n=strlen(a);
for(i=0;i<n-1;i++)
{
if(a[i]!='+'&&a[i]!='-'&&a[i]!='*'&&a[i]!='/')
{
c++;
cout<<"enter the value for\t"<<a[i]<<"\n";
cin>>b[i];
}}
r=n-c;
q=c-r;
if(q!=1)
{c
out<<"the expression is invalid\n";
getch();
}
else
{
c=c-1;
for(i=n-1;i>=0;i--)
{
switch (a[i])
{
case '+':b[c-1]=b[c-1]+b[c];
c=c-1;
break;
case '-':b[c-1]=b[c-1]-b[c];
c=c-1;
break;
case '*':b[c-1]=b[c-1]*b[c];
c=c-1;
break;
case '/':b[c-1]=b[c-1]/b[c];
c=c-1;
break;
}}
cout<<"the result is\n"<<b[c];
}
getch();
}


OUTPUT:
enter the postfix expression
abc+-
enter the value for a
10
enter the value for b
20
enter the value for c
10
the result is
20

PROGRAM TO IMPLEMENT TOWERS OF HANOI(DS)

Buzz It

#include<iostream.h>
#include<conio.h>
class hanoi
{
int n;
public:
void solution(char,char,char,int);
};
void hanoi::solution(char t1,char t2,char t3,int n)
{
if(n==1)
{
cout<<"move the top disk from
tower"<<"\t"<<t1<<"\t"<<"to"<<"\t"<<t2<<"\n";
return;
}
solution(t1,t3,t2,n-1);
cout<<"move the top disk from
tower"<<"\t"<<t1<<"\t"<<"to"<<"\t"<<t2<<"\n";
solution(t3,t2,t1,n-1);
}
void main()
{
clrscr();
hanoi ob;
int n;
char t1='a',t2='b',t3='c';
cout<<"enter number of disks\n";
cin>>n;
if(n<1)
{
cout<<"nothing to move";
}
else
{
ob.solution(t1,t2,t3,n);
}
getch();
}

OUTPUT:
enter number of disks
3
move the top disk from tower a to b
move the top disk from tower a to c
move the top disk from tower b to c
move the top disk from tower a to b
move the top disk from tower c to a
move the top disk from tower c to b
move the top disk from tower a to b

PROGRAM TO ILLUSTRATE QUEUE OPERATION USING ARRAY(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
class queue
{
int a[100],size,front,rear,i;
public:
void insertion();
void getsize();
void deletion();
void display();
};
void queue::getsize()
{
cout<<"enter the max size\n";
cin>>size;
front=rear=0;
}
void queue::insertion()
{
if(rear==size)
{
cout<<"over flow\n";
}
else
{
cout<<"enter data\n";
cin>>a[rear];
rear++;
}}
void queue::deletion()
{
if(front==rear)
{
cout<<"under flow\n";
front=rear=0;
}
else
{
front++;
}}
void queue::display()
{
cout<<'the elements in the array \n";
for(i=front;i<rear;i++)
cout<<a[i]<<"\t";
}
void main()
{
int c;
clrscr();
queue ob;
ob.getsize();
do
{
cout<<"\nmenu\n1-insertion\n2-deletion\n3-display\n4-exit\n";
cin>>c;
switch(c)
{
case 1:ob.insertion();
break;
case 2:ob.deletion();
break;
case 3:ob.display();
break;
}}
while(c<=3);
getch();
}


OUTPUT:
enter max size
2
menu
1-insertion
2-deletion
3-display
4-exit
1
enter data
58
menu
1-insertion
2-deletion
3-display
4-exit
1
enter data
67
menu
1-insertion
2-deletion
3-display
4-exit
3
the elements in the array
58 67
menu
1-insertion
2-deletion
3-display
4-exit
1
overflow
menu
1-insertion
2-deletion
3-display
4-exit
2
menu
1-insertion
2-deletion
3-display
4-exit
3
the elements in the array
67
menu
1-insertion
2-deletion
3-display
4-exit
2
menu
1-insertion
2-deletion
3-display
4-exit
2
underflow
menu
1-insertion
2-deletion
3-display
4-exit


PROGRAM TO ILLUSTRATE STACK OPERATION USING ARRAY(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
class stack
{
int top,n,a[50],i;
public:
void getsize();
void push();
void pop();
void display();
};
void stack::getsize()
{
top=-1;
cout<<"enter the size\n";
cin>>n;
}
void stack::push()
{
if(top==n-1)
{
cout<<"over flow\n";
}
else
{
cout<<"enter element\n";
top=top+1;
cin>>a[top];
}}
void stack::pop()
{
if(top==-1)
{
cout<<"under flow\n";
}
else
top--;
}
void stack::display()
{
cout<<"elements in the stack are\n";
for(i=top;i>=0;i--)
{
cout<<"\n"<<a[i];
}}
void main()
{
clrscr();
int c;
stack ob;
ob.getsize();
do
{
cout<<"menu\n1-push\n2-pop\n3-display\n4-exit\n";
cin>>c;
switch(c)
{
case 1:ob.push();
break;
case 2:ob.pop();
break;
case 3:ob.display();
break;
}}
while(c<=3);
getch();
}


OUTPUT:
enter the size
3
menu
1-push
2-pop
3-display
4-exit
1e
Enter the element
48
7
menu
1-push
2-pop
3-display
4-exit
1
overflow
menu
1-push
2-pop
3-display
4-exit
3e
Elements in the stack are
784
menu
1-push
2-pop
3-display
4-exit
2
menu
1-push
2-pop
3-display
4-exit
3e
Elements in the stack are
84


PROGRAM TO ILLUSTRATE LINEAR,BINARY AND INTERPOLATION SEARCHES(DS)

Buzz It

#include<iostream.h>
#include<conio.h>
class search
{
int a[100],size,i,j,temp;
public:
void linear(int);
void binary(int);
void interpolation(int);
void getarray();
void display();
void sort();
search()
{
size=0;
} };
void search::get array ()
{
cout<<"enter max size\n";
cin>>size;
cout<<"enter array elements\n";
for (i=0; i<size++)
{
cin>>a[i];
}}
void search::display ()
{
cout<<"the array is\n";
for (i=0; i<size-1; i++)
{
cout<<a[i];
}
}
void search::linear (int x)
{
int i;
for (i=0; i<size; i++)
}
if (a[i]==x)
{
cout<<"found key at the position\n"<<i;
return;
}
}
cout<<"not found\n";
}
void search::binary (int x)
{
int big=0;
int mid, end;
end=size-1;
while (big<=end)
{
mid= (big+end)/2;
if (a[mid]==x)
{
cout<<"found at\n"<<mid;
return;
}
else if (a[mid]<x)
{
big=mid+1;
}
else
end =mid-1;
}
cout<<"not found\n";
}
void search::sort ()
{
for(i=0;i<size-1;i++)
{
for(j=0;j<size-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"sorted list is \n";
for (i=0;i<size;i++)
cout<<a[i]<<"\n";
}
void search::interpolation (int x)
{
int mid;
int big=0;
int end=size-1;
while (big<=end)
{
mid=big+ ((x-a[big])*(end-big))/(a[mid]-a[big]);
if (a[mid]==x)
{
cout<<"element found at the position\n"<<mid<<"\n";
return;
}
else if (a[mid]<x)
{
big=mid+1;
}
else
end=mid-1;
}
cout<<"not found\n";
}
void main ()
{
int c,x;
clrscr ();
search ob;
do
{
cout<<"menu\n1-get array\n2-display\n3-binary\n4-linear\n5-
interpolation\n";
cin>>c;
switch(c)
{
case 1:ob.getarray();
break;
case 2:ob.display();
break;
case 3:cout<<"enter the element to search\n";
cin>>x;
ob.sort ();
ob.binary(x);
break;
case 4:cout<<"enter element to search\n";
cin>>x;
ob.linear(x);
break;
case 5:cout<<"enter the element to found\n";
cin>>x;
ob.sort();
ob.interpolation(x);
}}
while(c<=5);
getch ();
}


OUTPUT:
menu
1-get array
2-display
3-binary
4-linear
5-interpolation
1e
Enter max size
2e
Enter array elements
56
menu
1-get array
2-display
3-binary
4-linear
5-interpolation
2
the array is
56
menu
1-get array
2-display
3-binary
4-linear
5-interpolation
3e
Enter the element to search
5s
Sorted list is
56found at0

PROGRAM TO ILLUSTRATE BUBBLE SORT (DS)

Buzz It
# include<iostream.h>
#include<conio.h>
class name
{
int n,i,j,temp,a[100];
public:
void getdata();
void bubble();
};
void name::getdata()
{
cout<<"enter max size\n";
cin>>n;
cout<<"enter the numbers you want to sort\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}}
void name::bubble()
{
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}}}
cout<<"sorted list is\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\n";
}
void main()
{
clrscr();
name ob;
ob.getdata();
ob.bubble();
getch();
}


OUTPUT:
enter max size
3e
Enter the numbers you want to sort
45
12
78
sorted list is
12
45
78

BINARY TREE CREATION AND TRAVERSAL(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *left;
node *right;
};
class tree
{
public:
int a,b;
node *head,*root;
node *createnode(int a);
void createtree (node *root);
void inorder(node *root);
void preorder(node *root);
void postorder(node *root);
};
node* tree::createnode(int a)
{
head=new node;
head->data=a;
head->left=NULL;
head->right=NULL;
return(head);
}
void tree::createtree(node *root)
{
cout<<"do you want to create left child for"<<root->data<<"\n";
cin>>b;
if(b==1)
{
cout<<"enter data\n";
cin>>a;
root->left=createnode(a);
createtree(root->left);
}
cout<<"do you want to create right child for"<<root->data<<"\n";
cin>>b;
if(b==1)
{
cout<<"enter data\n";
cin>>a;
root->right=createnode(a);
createtree(root->right);
} }
void tree::inorder(node *root)
{
if(root!=NULL)
{
inorder(root->left);
cout<<root->data;
inorder(root->right);
}}
void tree::preorder(node *root)
{
if(root!=NULL)
{
cout<<root->data;
preorder(root->left);
preorder(root->right);
}}
void tree::postorder(node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
cout<<root->data;
}}
void main()
{
clrscr();
int a,c;
tree ob;
cout<<"enter root data\n";
cin>>a;
ob.root=ob.createnode(a);
do
{
cout<<"menu\n1-createtree\n2-inorder\n3-preorder\n4-postorder\n";
cin>>c;
switch(c)
{
case 1:ob.createtree(ob.root);
break;
case 2:ob.inorder(ob.root);
break;
case 3:ob.preorder(ob.root);
break;
case 4:ob.postorder (ob.root);
break;
}}
while(c<=4);
getch();
}

OUTPUT:
enter root data
5
menu
1-createtree
2-inorder
3-preorder
4-postorder
1
do you want to create left child for5
1e
Enter data
4
do you want to create left child for4
1e
Enter data
3
do you want to create left child for3
0
do you want to create right child for3
0
do you want to create right child for4
1e
Enter data
6
do you want to create left child for6
0
do you want to create right child for6
0
do you want to create right child for5
1e
Enter data
7
do you want to create left child for7
0
do you want to create right child for7
0
menu
1-createtree
2-inorder
3-preorder
4-postorder
23
4657
menu
1-createtree
2-inorder
3-preorder
4-postorder
3
54367
menu
1-createtree
2-inorder
3-preorder
4-postorder
43
6475

PREFIX & POSTFIX EVALUATION

Buzz It

/*PROGRAM TO INCREMENT A VALUE BY OVERLOADING ++
OPERATOR(PREFIX & POSTFIX )*/


#include<iostream.h>
#include<conio.h>
class number
{
int a,temp;
public:
number();
int display();
number operator++(void);
number operator++(int);
};
number::number()
{
a=0;
}
int number::display(void)
{
return a;
}
number number::operator++(void)
{
number temp;
temp.a=++a;
return temp;
}
number number::operator++(int)
{
number temp;
temp.a=a++;
return temp;
}
void main()
{
number s1,s2;
clrscr();
cout<<"s1="<<s1.display()<<"and s2="<<s2.display();
++s1;
s1++;
s2=s1++;
cout<<"\nafter applying increment operator\n";
cout<<"s1="<<s1.display()<<"and s2="<<s2.display();
getch();
}


OUTPUT:
s1=0 and s2=0
after applying increment operator
s1=3 and s2=2

STUDENT DETAILS

Buzz It
/*PROGRAM TO READ AND DISPLAY STUDENT DETAILS
USING CLASSES */


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int rollno,i;
char name[20];
float mark[6],total,avg;
public:
void read();
void calc();
void result();
};
void student::read()
{
cout<<"enter the roll no\t";
cin>>rollno;
cout<<"enter the name\t";
cin>>name;
cout<<"enter the mark in 6 subject\n";
for(i=0;i<6;i++)
cin>>mark[i];
}
void student::calc()
{ total=0;
for(i=0;i<6;i++)
total=total+mark[i];
avg=total/6;
}
void student::result()
{
cout<<"\nroll no is\t"<<rollno;
cout<<"\nname is\t"<<name;
for(i=0;i<6;i++)
{
cout<<"\nmark in\t"<<i+1<<"subject:";
cout<<mark[i]<<"\n";
}
cout<<"\ntotal is:"<<total;
cout<<"\n average is:"<<avg;
}
void main()
{
clrscr();
char choice;
student st;
do
{
cout<<"\nenter the information of the student\n";
st.read();
cout<<"\nINFORMARTION:";
st.calc();
st.result();
cout<<"\ndo u want to continue or not(Y or N)\n";
cin>>choice;
}
while(choice=='Y'choice=='y');
getch();
}


OUTPUT:
enter the information of the student
enter the roll no 264
enter the name Anoop
enter the mark in 6 subject
45
40
42
38
60
55
INFORMARTION:
roll no is 264
name is Anoop
mark in 1 subject:45
mark in 2 subject:40
mark in 3 subject:42
mark in 4 subject:38
mark in 5 subject:60
mark in 6 subject:55
total is: 280
average is:46.666668
do u want to continue or not(Y or N)
N

BOOKSHOP INVENTORY

Buzz It
/*PROGRAM TO ILLUSTRATE BOOKSHOP INVENTORY USING
CLASSES*/


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class book
{
char name[25],author[25],pub[25],type[25];
float price,disc,org;
int pgno;
public:
void read();
void calc();
void display();
void show();
};
void book::read()
{
cout<<"ENTER THE DETAILS OF BOOK\n";
cout<<"----------------------------";
cout<<"\nEnter the Name:";
gets(name);
cout<<"\nEnter the name of Author:";
gets(author);
cout<<"\nEnter the name of Publication:";
gets(pub);
cout<<"\nEnter the type of Book:";
gets(type);
cout<<"\nEnter the page no:";
cin>>pgno;
cout<<"\nEnter the original price:";
cin>>org;
cout<<"\nEnter the discount:";
cin>>disc;
}
void book::display()
{
cout<<"\nPUBLICATION DETAILS\n";
cout<<"---------------------------";
cout<<"\nName:\t\t"<<name;
cout<<"\nAuthor:\t\t"<<author;
cout<<"\nPublication:\t"<<pub;
cout<<"\nType :\t\t"<<type;
cout<<"\npageno:\t\t"<<pgno;
}
void book::calc()
{
price =org-(disc/100*org);
}
void book::show()
{
cout<<"\nPrice:\t\t"<<price;
}
void main()
{
char ch;
book ob;
clrscr();
do
{
ob.read();
ob.calc();
ob.display();
ob.show();
cout<<"\nDo You Want to Continue?";
cout<<"\nY or N";
cin>>ch;
}
while(ch=='y'ch=='Y');
getch();
}


OUTPUT
ENTER THE DETAILS OF BOOK
-------------------------------------------
Enter the Name: Ente kadha
Enter the name of Author: Kamalasurayya
Enter the name of Publication: Surya
Enter the type of Book: Autobiography
Enter the page no:320
Enter the original price: 100
Enter the discount:30
PUBLICATION DETAILS
---------------------------------
Name: Ente kadha
Author: Kamalasurayya
Publication: Surya
Type of book : Autobiography
pageno:320
price:70
Do You Want to Continue?
Y or N
N


PUBLICATION DETAILS

Buzz It
/*PROGRAM TO READ AND DISPLAY PUBLICATION DETAILS
USING INHERITANCE */


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class book
{
protected:
char name[25],author[25],pub[25],type[25];
int pgno;
public:
void read();
void display();
};
void book::read()
{
cout<<"ENTER THE DETAILS OF BOOK\n";
cout<<"----------------------------";
cout<<"\nEnter the Book Name:";
gets(name);
cout<<"\nEnter the name of Author:";
gets(author);
cout<<"\nEnter the name of Publication:";
gets(pub);
cout<<"\nEnter the type of Book:";
gets(type);
cout<<"\nEnter the page no:";
cin>>pgno;
}
void book::display()
{
cout<<"\nPUBLICATION DETAILS\n";
cout<<"---------------------------";
cout<<"\nName:\t\t"<<name;
cout<<"\nAuthor:\t\t"<<author;
cout<<"\nPublication:\t"<<pub;
cout<<"\nType :\t\t"<<type;
cout<<"\npageno:\t\t"<<pgno;
}
class price:public book
{float price,disc,org;
public:
void readp();
void calc();
void dis();
};
void price::readp()
{
cout<<"\nEnter the original price:";
cin>>org;
cout<<"\nEnter the discount:";
cin>>disc;
}
void price::calc()
{
price =org-(disc/100*org);
}
void price::dis()
{
cout<<"\nPrice:\t\t"<<price;
}
void main()
{
char ch;
price p;
clrscr();
do
{
p.read();
p.readp();
p.calc();
p.display();
p.dis();
cout<<"\nDo You Want to Continue?";
cout<<"\nY or N";
cin>>ch;
}
while(ch=='y'ch=='Y');
getch();
}

OUTPUT:
ENTER THE DETAILS OF BOOK
-------------------------
Enter the Book Name:Merchant of Venice
Enter the name of Author:Shakesphere
Enter the name of Publication:ABC publications
Enter the type of Book:Shortstory
Enter the page no:78
Enter the original price:100
Enter the discount:5
PUBLICATION DETAILS
---------------------------
Name: Merchant of venice
Author: Shakesphere
Publication: ABC publication
Type: Shortstory
Price: 95
Do you want to continue?
Y or N
N

PROGRAM TO COPY STRINGS USING POINTERS

Buzz It
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char*s;
cout<<"enter the string\n";
gets(s);
char*p=" ";
cout<<"\nthe string "<<s<<" is in "<<&s<<"\n";
p=s;
cout<<"\ncopied to "<<&p<<"\nstring is\t"<<p<<"\n";
getch();
}


OUTPUT:
enter the string
farook
the string is in 0*8fcefff4
copied to 0*8fcefff2
string is farook

PROGRAM TO CONCATENATE TWO STRINGS BY OVERLOADING += OPERATOR

Buzz It
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class string
{
char str[100];
public:
void input();
void output();
string operator+(string s);
};
void string::input()
{
cout<<"enter the string\n";
gets(str);
}s
tring string::operator+(string s)
{
string temp;
strcpy(temp.str,str);
strcat(temp.str,s.str);
return(temp);
}
void string::output()
{
cout<<"the string is\n";
cout<<str;
}
void main()
{
string s1,s2,s3;
clrscr();
s1.input();
s2.input();
s3=s1+s2;
s3.output();
getch();
}


OUTPUT:
enter the string
farook
enter the string
college
the string is
farookcollege

PROGRAM TO ILLUSTRATE BASE CONVERSION

Buzz It
#include<iostream.h>
#include<math.h>
#include<conio.h>
class number
{
long int dec,i,d,n,j;
long bin,num,b[50];
public:
void bin_to_dec();
void dec_to_bin();
};
void number::bin_to_dec()
{
dec=0;
i=0;
cout<<"enter the binary no\n";
cin>>num;
bin=num;
for(i=0;bin>0;i++)
{
d=bin%10;
dec=dec+(d*pow(2,i));
bin=bin/10;
}
cout<<"\ndecimal equivalent of"<<num<<"is:"<<dec;
}
void number::dec_to_bin()
{
cout<<"enter the decimal number\n";
cin>>n;
num=n;
for(i=0;num>0;i++)
{
b[i]=num%2;
num=num/2;
}
cout<<"the binary number equivalent to"<<n<<"is";
for(i=i-1;i>=0;i--)
{
cout<<b[i];
}}
void main()
{
int ch;
clrscr();
number ob;
do
{
cout<<"\nmenu\n1-binary to decimal\n2-decimal to binary\n3-exit\n";
cin>>ch;
switch(ch)
{
case 1:
ob.bin_to_dec();
break;
case 2:
ob.dec_to_bin();
break;
}}
while(ch<3);
getch();
}

OUTPUT:
menu
1-binary to decimal
2-decimal to binary
3-exit
1e
Enter the binary no
1010
decimal equivalent of 1010 is 10
menu
1-binary to decimal
2-decimal to binary
3-exit
2e
Enter the decimal number
7
the binary number equivalent to 7 is 111
menu
1-binary to decimal
2-decimal to binary
3-exit
3


PROGRAM TO FIND SUM OF TWO LENGTHS EXPRESSED IN FEET AND INCH

Buzz It
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int f1,f2,f,i1,i2,i,s,x,y;
cout<<"enter the first length in feet and inch\n";
cin>>f1>>i1;
cout<<"enter the second length in feet and inch\n";
cin>>f2>>i2;
f=f1+f2;
i=i1+i2;
x=i/12;
y=i%12;
s=f+x;
cout<<"sum of two length in feet and inch
is\n"<<s<<"feet"<<"\t"<<y<<"inch”; getch();
}

OUTPUT:
enter the first length in feet and inch
2 6
enter the second length in feet and inch
3 8
sum of two length in feet and inch is
6feet 2inch


PROGRAM FOR DISTANCE CALCULATION

Buzz It
/*PROGRAM TO FIND THE SUM OF DISTANCE EXPRESSED IN
KILOMETRE,METRE AND CENTIMETRE*/


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int k1,k2,k,m1,m2,m,me,c1,c2,c,b,d,a,cm;
cout<<"enter the first length in km,m,cm\n";
cin>>k1>>m1>>c1;
cout<<"enter the second length in km,m,cm\n";
cin>>k2>>m2>>c2;
c=c1+c2;
a=c/100;
cm=c%100;
m=m1+m2+a;
d=m/1000;
me=m%1000;
k=k1+k2+d;
cout<<"sum is\n"<<k<<"km"<<"\t"<<me<<"metre"<<"\t"<<cm<<"cm";
getch();
}


OUTPUT:
enter the first length in km,m,cm
2 50 120
enter the second length in km,m,cm
1 50 100
sum is
3Km 102metre 20cm


TIME CONVERSION

Buzz It
/*PROGRAM TO CONVERT TIME EXPRESSED IN
HOURS,MINUTES AND SECONDS TO SECONDS*/

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int h1,m1,s1,sec;
cout<<"enter the time in hour,min,sec\n";
cin>>h1>>m1>>s1;
sec=(h1*60*60)+(m1*60)+s1;
cout<<"the time in seconds is\n"<<sec;
getch();
}

OUTPUT:
enter the time in hour,min,sec
1 30 2
the time in seconds is
5402

PROGRAM TO ADD TWO AMOUNTS EXPRESSED IN RUPEES

Buzz It
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int pa,r1,r2,p1,p2,p,a,r;
cout<<"enter the first amount in rupees and paise\n";
cin>>r1>>p1;
cout<<"enter the second amount in rupees and paise\n";
cin>>r2>>p2;
p=p1+p2;
a=p/100;
pa=p%100;
r=r1+r2+a;
cout<<r<<"rupees"<<"\t"<<pa<<"paise";
getch();
}


OUTPUT:
enter the first amount in rupees and paise
1 50
enter the second amount in rupees and paise
20 70
22rupees 20paise

PROGRAM TO GENERATE STRANGE NUMBERS UP TO ‘N’

Buzz It
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{
long int x,s,a,n,num;
clrscr();
cout<<"up to which number\n";
cin>>n;
cout<<"strange numbers up to \t"<<n<<" are:\n";
for(num=1;num<=n;++num)
{
s=0;
x=num;
while(x>0)
{
a=x%10;
s=s+pow(a,3);
x=x/10;
}
if(num==s)
cout<<num<<"\t";
}
getch();
}


OUTPUT:
up to which number
4000
strange numbers up to 4000 are:
1 153 370 371 407

PROGRAM TO PRINT PRIME NUMBERS UP TO ‘N’

Buzz It
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,num,i,flag;
clrscr();
cout<<"enter the limit\n";
cin>>n;
cout<<"the prime numbers up to\t"<<n<<" are:";
for(num=2;num<=n;num++)
{
flag=1;
for(i=2;i<=sqrt(num);i++)
if(num%i==0)
{
flag=0;
break;
}
if(flag)
{cout<<"\t"<<num;
}}
getch();
}

OUTPUT:
enter the limit
25
the prime numbers up to 25 are:
2 3 5 7 11 13 17 19 23