You are on page 1of 25

/* Program to search an element of an array using Binary search */

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int A[50], n,p;
cout<<"Enter the Size of array : ";
cin>>n;
cout<<"Enter the elements in ascending or descending order : ";
for(int i=0; i<n; i++)
cin>>A[i];
cout<<"\nArray formed is : ";
for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\nEnter the element to be searched : ";
cin>>p;
void binary_search(int A[], int n, int p);
binary_search(A,n,p);
getch();
}

void binary_search(int A[], int n, int p)


{ int L,U,mid; char ch;
lb: L=0; U=n-1;

while(L<=U)
{ mid=(L+U)/2;
if(A[mid]==p)
{ cout<<"\nElement "<<p<<" found. Search Successful.";
cout<<"\nSubscript = "<<mid<<" \nPosition = "<<mid+1;
break;
}
else if(p<=A[mid])
U=mid-1;

else
L=mid+1;
}
if(L>U)
{cout<<"\nUnsuccessful search.";
cout<<"\nWant to search again. : ";
cin>>ch;
if(ch=='y'||ch=='Y')
{cout<<"\nEnter the element to be searched : ";
cin>>p;
goto lb;}

}
}
Output:
Enter the Size of array : 5
Enter the elements ascending or descending order:
2
5
8
9
11

Array formed is : 2 5 8 9 11
Enter the element to be searched : 8

Element 8 found. Search Successful.


Subscript = 2
Position = 3
/* Wap in c++ to create a binary file and write objects of class BOOK to it and display them on
screen after reading from the file. */
#include<fstream.h>
#include<conio.h>
int p;
class book {
int book_no;
char book_title[80];
float price;
float total_cost(int n)
{
float p;
p=n*price;
return p;
}

public :
void input()
{ cout<<"\nEnter Book no. : "; cin>>book_no;
cout<<"\nEnter book title : ";
cin.get();
cin.getline(book_title,80);
cout<<"\nEnter book price : "; cin>>price;
}

void purchase()
{ cout<<"\nEnter number of copies to be purchased : "; cin>>p;
cout<<"\nTotal cost (in Rs ) = "<<total_cost(p)<<"\n";
}

void display()
{ cout<<"\nBook title is : "<<book_title;
cout<<"\nBook number is : "<<book_no;
cout<<"\nPrice of book is : "<<price;
cout<<"\nTotal cost of "<<p<<" books => "<<total_cost(p)<<" Rs\n";
} };
void main()
{clrscr();
book b[3],p[3];
fstream bo;
cout<<"---------------------------------------------------------Enter Details----------------------------------------------“;
for(int i=0; i<3; i++)
{
b[i].input();
b[i].purchase();
bo.open("xy.txt",ios::out|ios::binary);
bo.write((char *)&b[i],sizeof(b[i]));
bo.close();
bo.open("xy.txt",ios::in |ios::binary);
bo.read((char *)&p[i], sizeof(p[i]));
bo.close();
}
cout<<"\n--------------------------------------------------- Book Details --------------------------------------------------
“;
for(i=0; i<3; i++)
{ p[i].display(); }
getch();
}
Output:
--------------------------------------------------------------------- Enter Details ----------------------------------------------

Enter Book no. : 1


Enter book title : H.C verma
Enter book price : 200
Enter number of copies to be purchased : 5
Total cost (in Rs ) = 1000

Enter Book no. : 2


Enter book title: Sumita arora
Enter book price: 250
Enter number of copies to be purchased: 5
Total cost (in Rs) = 1250

Enter Book no. : 3


Enter book title : R.D sharma
Enter book price : 250
Enter number of copies to be purchased : 2
Total cost (in Rs ) = 500

------------------------------------------------------------------ Book Details -------------------------------------------------

Book title is : H.C verma


Book number is : 1
Price of book is : 200
Total cost of 2 books => 400 Rs

Book title is : Sumita arora


Book number is : 2
Price of book is : 250
Total cost of 2 books => 500 Rs

Book title is : R.D sharma


Book number is : 3
Price of book is : 250
Total cost of 2 books => 500 Rs
/* Wap in C++ to implement queue as an array */

#include<iostream.h>
#include<conio.h>
const int size=50;
int q[size],rear=0,front=0;
char ch;
void insert(int q[],int ele)
{
if(rear>=size)
{ cout<< “queue is full”;getch(); }
else if(rear==0)
{
front=rear=1;
q[rear]=ele;
}
else
{
rear++;
q[rear]=ele;
};
}

int delet(int q[])


{
int r ;
if(front==0 && rear==0)
{ cout<<”queue is empty”; getch(); }
else
{
r=q[front];
if(rear==front)
front=rear=0;
else
front++;
}
return r;
}

void display(int q[],int front,int rear)


{
for(int i=front;i<=rear;i++)
cout<<q[i]<<"-- > ";
}

void main()
{
clrscr();
int n,u,m;
char ans;
do
{
cout<<"\nChoose from the menu :\n"
<<"\n 1. Insert"
<<"\n 2. Delete"
<<"\n 3. Display"
<<"\n\n Enter your choice : "; cin>>n;

switch(n)
{
case 1: ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n Enter element to be inserted :"; cin>>m;
insert(q,m);
cout<<"\n The resultant Queue is : ";
display(q,front,rear);
cout<<"\n\n Want to enter again ?: ";
cin>>ans;
}
break;

case 2: ans='y';
while(ans=='y'||ans=='Y')
{
u=delet(q);
{ cout<<"\n The deleted element is "<<u<<"\n";
cout<<"\n The resultant Queue is : \n\n";
display(q,front,rear);
}
cout<<"\n\n Want to delete again ?: ";
cin>>ans;

}
break;

case 3: cout<<"\n The Queue is : \n\n";


display(q,front,rear);
break;

default: cout<<"\n WRONG CHOICE, Please enter desired keyword : ";


}

cout<<"\n Choose from the menu again ? : ";cin>>ch;

} while(ch=='y'||ch=='Y'); getch();
}
Output:
Choose from the menu :
1. Insert
2. Delete
3. Display
Enter your choice : 1

Enter element to be inserted :1


The resultant Queue is : 1-->
Want to enter again ?: y

Enter element to be inserted :2


The resultant Queue is : 1-->2-->
Want to enter again ?: y

Enter element to be inserted :3


The resultant Queue is : 1-->2-->3-->
Want to enter again ?: n

Choose from the menu again ? : y

Choose from the menu :


1. Insert
2. Delete
3. Display
Enter your choice : 2

The deleted element is 1


The resultant Queue is :2-->3-->
Want to delete again ?: y

The deleted element is 2


The resultant Queue is :3-->
Want to delete again ?: n

Choose from the menu again ? : y


Choose from the menu :
1. Insert
2. Delete
3. Display
Enter your choice : 3

The Queue is :3-->


Choose from the menu again ? :n
/* Wap to print and find the sum of Fibonacci series using recursion. */

#include<iostream.h>
#include<conio.h>
int fibonacci(int n);
void main()
{
clrscr();
int n;
cout<<"\n Enter the number of terms upto which you want the sum of fibonnaci series : ";
cin>>n;
cout<<"\nThe fibonacci series generated is : \n";
cout<<"1 1 ";
int s=fibonacci(n);
cout<<"\nThe sum of fibonacci series for "<<n<<" terms = "<<s;
getch();

}
int first=1;
int second=1;
int third;
int i=2;
int sum=0;
int fibonacci(int n)
{ if(n==1)
sum=1;
else if(n==2)
sum=2;
else
if(n>1 && i!=n)
{ third=first+second;
cout<<third<<” “;

if(i==2)
sum+=first+second+third;
else
sum+=third;
first=second;
second=third;
++i;
fibonacci(n);}
return sum ;}
Output :
Enter the number of terms upto which you want the sum of fibonnaci series : 5
The fibonacci series generated is :
1 1 2 3 5
The sum of fibonacci series for 5 terms = 12
/* WAP in c++ to perform basic operations on the two complex numbers using structures. */

#include<iostream.h>
#include<math.h>
#include<conio.h>
struct complex {
int i,r;
}x,y;

void add(complex a, complex b)


{
cout<<"\n Resultant of addition of complex numbers = "<<a.r+b.r<<" + ("<<a.i+b.i<<")i ";
}

void subtract(complex a,complex b)


{int ch;
do{
cout<<"\n\t1.Subtract complex 2 from complex 1 : ";
cout<<"\n\t2.Subtract complex 1 from complex 2 : ";
cout<<"\n\tEnter your choice : "; cin>>ch;
switch(ch)
{
case 1 : cout<<"\nResultant of subtraction of complex number => "<<"("<<a.r-b.r<<")"<<" +
("<<a.i-b.i<<") i"; break;
case 2 : cout<<"\nResultant of subtraction of complex number => "<<"("<<b.r-a.r<<")"<<" +
("<<b.i-a.i<<") i"; break;
default : cout<<"\n WRONG CHOICE. ";
}}while (ch==1 &&ch==2);
}

void multiply(complex a, complex b)


{
cout<<"\nResultant of multiplication of complex number => "<<"("<<a.r*b.r-a.i*b.i<<")"<<" +
("<<a.r*b.i+a.i*b.r<<") i";
}

void divide(complex a,complex b)


{int ch;
do{ cout<<"\n\t1.Divide complex 2 from complex 1 : ";
cout<<"\n\t2.Divide complex 1 from complex 2 : ";
cout<<"\n\tEnter your choice : "; cin>>ch;
switch(ch)
{
case 1 : cout<<"\n\nResultant of division of complex number =>
"<<((a.r*b.r)+(a.i*b.i))/(pow(b.r,2)+pow(b.i,2))
<<" + ("<<((a.i*b.r)-(a.r*b.i))/(pow(b.r,2)+pow(b.i,2))<<") i";
break;
case 2 : cout<<"\n\nResultant of division of complex number =>
"<<((b.r*a.r)+(b.i*a.i))/(pow(a.r,2)+pow(a.i,2))
<<" + ("<<((b.i*a.r)-(b.r*a.i))/(pow(a.r,2)+pow(a.i,2))<<") i";
break;
default : cout<<"\n\nPlease enter desired keyword. ";
}}while(ch==1 && ch==2);
}
void main()
{
clrscr();
int choice;
char ch;

cout<<"Complex number 1 : ";


cout<<"\n\tEnter the real part : "; cin>>x.r;
cout<<"\n\tEnter the imaginary part : "; cin>>x.i;
cout<<"\n";
cout<<"Complex number 1 -> "<<x.r<<"+("<<x.i<<")i";
cout<<"\nComplex number 2 : ";
cout<<"\n\tEnter the real part : "; cin>>y.r;
cout<<"\n\tEnter the imaginary part : "; cin>>y.i;
cout<<"\n";
cout<<"\nComplex number 2 -> "<<y.r<<"+("<<y.i<<")i";

do
{cout<<"\nChoose from the folowing : ";
cout<<"\n1. Add two complex numbers ";
cout<<"\n2. Subtract two complex numbers ";
cout<<"\n3. Multiply two complex numbers ";
cout<<"\n4. Divide two complex numbers ";
cout<<"\nEnter your choice : "; cin>>choice;
switch(choice)
{
case 1:add(x,y); break;
case 2:subtract(x,y); break;
case 3:multiply(x,y); break;
case 4:divide(x,y); break;
}
cout<<"\nWant to Choose again -> ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
Output:
Complex number 1
Enter the real part : 2
Enter the imaginary part : 5
Complex number 1 -> 2+(5)i

Complex number 2 :
Enter the real part : 1
Enter the imaginary part : 3
Complex number 2 -> 1+(3)i

Choose from the folowing :


1. Add two complex numbers
2. Subtract two complex numbers
3. Multiply two complex numbers
4. Divide two complex numbers

Enter your choice : 1


Resultant of addition of complex numbers = 3 + (8)i

Want to Choose again -> Y

Choose from the folowing :


1. Add two complex numbers
2. Subtract two complex numbers
3. Multiply two complex numbers
4. Divide two complex numbers

Enter your choice : 2


1. Subtract complex 2 from complex 1 :
2. Subtract complex 1 from complex 2 :
Resultant of subtraction of complex number -> (1) + (2) i

Want to Choose again -> n

Choose from the folowing :


1. Add two complex numbers
2. Subtract two complex numbers
3. Multiply two complex numbers
4. Divide two complex numbers

Enter your choice : 3


Resultant of multiplication of complex number -> (-13) + (11) i

Want to Choose again -> y

Choose from the folowing :


1. Add two complex numbers
2. Subtract two complex numbers
3. Multiply two complex numbers
4. Divide two complex numbers

Enter your choice : 4


1. Divide complex 2 from complex 1
2. Divide complex 1 from complex 2 :
Resultant of division of complex number -> 1.7 + (-0.1) i
Want to Choose again -> N

/* WAP USING FUNCTION OVERLOADING TO CALCULATE AREA OF CIRCLE, SQUARE, RECTANGLE,


RIGHT TRIANGLE AND TRIANGLE USING HERON’S FORMULAE */

#include<iostream.h>
#include<math.h>
#include<conio.h>
float area(float p)
{
return 3.14*p*p;
}

float area(int p)
{
return p*p;
}

float area(float p,float q)


{
return p*q;
}

float area(int p, int q)


{
return (0.5*p*q);
}

float area(float p,float q, float r)


{
float s;
s=(p+q+r)/2;
return sqrt(s*(s-p)*(s-q)*(s-r));
}

void main()
{clrscr();
int p,q,ch;
float a,b,r;
char choice;
do{
cout<<"\nChoose from the following : ";
cout<<"\n1. Area of square ";
cout<<"\n2. Area of circle ";
cout<<"\n3. Area of rectangle ";
cout<<"\n4. Area of right triangle ";
cout<<"\n5. Area of triangle ";
cout<<"\nEnter your choice : "; cin>>ch;

switch(ch)
{
case 1: cout<<"\nEnter side of square : ";
cin>>p;
cout<<"area of square is : "<<area(p);
break;
case 2: cout<<"\nEnter radius of circle : ";
cin>>a;
cout<<"area of circle is : "<<area(a);
break;

case 3: cout<<"\nEnter length of rectangle : ";


cin>>a;
cout<<"Enter breadth of rectangle : ";
cin>>b;
cout<<"area of rectangle is : "<<area(a,b);
break;

case 4: cout<<"\nEnter base and altitude of right triangle : ";


cin>>p;
cin>>q;
cout<<"area of right triangle is : "<<area(q,p);
break;

case 5: cout<<"\nEnter sides of triangle : ";


cin>>a;
cin>>b;
cin>>r;
cout<<"area of triangle is : "<<area(a,b,r);
break;

}
cout<<"\nWant to choose again : ";
cin>>choice;
}while(choice=='y'||choice=='Y');

getch();

}
Output:

Choose from the following :


1. Area of square
2. Area of circle
3. Area of rectangle
4. Area of right triangle
5. Area of triangle
Enter your choice : 1

Enter side of square : 5


area of square is : 25
Want to choose again : y

Choose from the following :


1. Area of square
2. Area of circle
3. Area of rectangle
4. Area of right triangle
5. Area of triangle
Enter your choice : 2

Enter radius of circle : 2.5


area of circle is : 19.625
Want to choose again : y

Choose from the following :


1. Area of square
2. Area of circle
3. Area of rectangle
4. Area of right triangle
5. Area of triangle
Enter your choice : 3

Enter length of rectangle : 4.2


Enter breadth of rectangle : 5.1
area of rectangle is : 21.419998
Want to choose again : y

Choose from the following :


1. Area of square
2. Area of circle
3. Area of rectangle
4. Area of right triangle
5. Area of triangle
Enter your choice : 4

Enter base and altitude of right triangle : 2 5


area of right triangle is : 5
Want to choose again : y

Choose from the following :


1. Area of square
2. Area of circle
3. Area of rectangle
4. Area of right triangle
5. Area of triangle
Enter your choice : 5
Want to choose again :n
/* Wap in c++ to implement Queue as a linked list */
#include<iostream.h>
#include<conio.h>
struct node {
int roll;
node* next;
}*front,*rear,*ptr,*newptr,*np;

node *create(int a)
{
ptr=new node;
ptr->roll=a;
ptr->next=NULL;
return ptr;
}

void insert(node *np)


{
if(front==NULL)
front=rear=np;
else
{
rear->next=np;
rear=np;
}
}

void delet()
{
if(front==NULL)
cout<<"\n Underflow!!!!";
else
{
ptr=front;
front=front->next;
delete ptr;
}
}

void display(node *np)


{
while(np!=NULL)
{
cout<<np->roll<<"-> ";
np=np->next;
}
}

void main()
{
clrscr();
front=rear=NULL;
int n,m;
char ans,ch;
do
{ cout<<"\nChoose from the menu : "
<<"\n 1) Insert."
<<"\n 2) Delete
<<"\n 3) Display."
<<"\n\n Enter your choice : ";
cin>>n;
switch(n)
{
case 1: ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n Enter element to be inserted .";
cin>>m;

newptr=create(m);

if(newptr==NULL)
cout<<"\n Cannot create !!!!";

insert(newptr);

cout<<"\n The Queue formed is : ";

display(front);

cout<<"\n Want to enter more nodes ?: ";


cin>>ans;
}
break;

case 2: ans='y';
while(ans=='y'||ans=='Y')
{
delet();
cout<<"\n Queue : ";
display(front);
cout<<"\n Want to delete more ?: ";
cin>>ans;
}
break;

case 3: cout<<"\n Queue : ";


display(front);
break;

default: cout<<"\n You entered wrong choice...";


}

cout<<"\n Want to return to main menu ? : ";


cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
OUTPUT

Choose from the menu :


1. Insert
2. Delete
3. Display

Enter your choice : 1

Enter element to be inserted : 1

The Queue formed is : 1-->


Want to enter more nodes ?: y

Enter element to be inserted .2

The Queue formed is :1-> 2->


Want to enter more nodes ?: y

Enter element to be inserted .3

The Queue formed is : 1-> 2-> 3->


Want to enter more nodes ?: n

Want to return to main menu ? : y

Choose from the menu :


1. Insert
2. Delete
3. Display

Enter your choice : 2

Queue :2-> 3->

Want to delete more ?: n

Want to return to main menu ? : y

Choose from the menu : n


1. Insert
2. Delete
3. Display

Enter your choice : 3

Queue : 2-> 3->

Want to return to main menu ? : n


/* Wap to implement stack as a linked list */

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

struct node {
int roll;
node* next;
}*top,*save,*ptr,*newptr,*np;

node *create(int a)
{
ptr=new node;
ptr->roll=a;
ptr->next=NULL;
return ptr;
}

void push(node *np)


{
if(top==NULL)
top=np;
else
{
save=top;
top=np;
np->next=save;
}
}

void pop()
{
if(top==NULL)
cout<<"\n Underflow!!!!";
else
{
ptr=top;
top=top->next;
delete ptr;
}
}

void display(node *np)


{
while(np!=NULL)
{
cout<<np->roll<<" -> ";
np=np->next;
}
}

void main()
{
clrscr();
top=NULL;
int n,m;
char k,ch;

do {
cout<<"\nChoose from the menu :\n"

<<"\n 1. Push."
<<"\n 2. Pop."
<<"\n 3. Display."
<<"\n 4. Quit."
<<"\n\nEnter your choice : ";
cin>>n;
switch(n)
{
case 1: k='y';
while(k=='y'||k=='Y')
{
cout<<"\n Enter element to be inserted .";
cin>>m;
newptr=create(m);

if(newptr==NULL)
cout<<"\n Cannot create !!!!";

push(newptr);
cout<<"\n The Stack formed is : ";

display(top);

cout<<"\n\n Want to enter again ?: ";


cin>>k;
}
break;

case 2: k='y';
while(k=='y'||k=='Y')
{
pop();
cout<<"\n The Stack formed is : \n\n";
display(top);
cout<<"\n\n Want to delete again ?: ";
cin>>k;
}
break;

case 3: cout<<"\n The Stack formed is : ";


display(top);
break;

case 4: exit(0);
break;

default: cout<<"\n Please enter desired keyword : ";


}
cout<<"\n Do you want to continue..? : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}

Output:
Choose from the menu :
1. Push.
2. Pop.
3. Display.
4. Quit.

Enter your choice : 1

Enter element to be inserted : 1

The Stack formed is : 1 ->

Want to enter again ?: y

Enter element to be inserted : 2

The Stack formed is : 2 -> 1 ->

Want to enter again ?: y

Enter element to be inserted : 3

The Stack formed is : 3 -> 2 -> 1 ->

Want to enter again ?: n

Do you want to continue..? : y

Choose from the menu :


1. Push.
2. Pop.
3. Display.
4. Quit.

Enter your choice : 2

The Stack formed is : 2 -> 1 ->

Want to delete again ?: y

The Stack formed is :1 ->

Want to delete again ?: N

Do you want to continue..? : n

Choose from the menu :


1. Push.
2. Pop.
3. Display.
4. Quit.

Enter your choice : 3


The Stack formed is : 1 ->

Do you want to continue..? : n


/* Wap in c++ using pointers to find the smallest and the largest element in a dynamically created
array.*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *array, smallest, largest, n;
cout<<"\nEnter the number of elements : ";
cin>>n;
array=new[n];
cout<<"\nEnter the elements : \n";
for(int i=0; i<n; i++)
cin>>array[i];
i=0;
cout<<"\nThe array formed is : \n";
while(i!=n)
{
cout<<array[i]<<" ";
i++;
}
smallest=array[0];
for(i=0; i<n; i++)
{
if(array[i]<=smallest)
smallest=array[i];
}
largest=array[0];
for(i=0; i<n; i++)
{
if(array[i]>=largest)
largest=array[i];
}

cout<<"\nThe smallest element is : "<<smallest<<"\nThe largest element is : "<<largest;

getch(); }
Output :
Enter the number of elements : 5
Enter the elements :
1
9
2
8
6
The array formed is :

1 9 2 8 6
The smallest element is : 1
The largest element is : 9
/* wap using pointers to find the length of a string and print the reversed string . */

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

void main()
{
clrscr();
char *str= new char[256];

cout<<"\n\nEnter the string : ";


cin.getline(str,256);

cout<<"\nThe string length -> "<<strlen(str);

cout<<"\nThe reverse string is : ";

char intermediate;

for(int j=strlen(str)-1,i=0; i<=strlen(str)/2; j--,i++)


{
intermediate = *(str+j);

*(str+j)=*(str+i);

*(str+i)=intermediate;
}
cout<<str;

getch();
}
Output :

Enter the string : pranavbhardwaj076@gmail.com

The string length -> 25

The reverse string is : moc.liamg@670jawdrahbvanarp

You might also like