0% found this document useful (0 votes)
4 views37 pages

C++ Program

Uploaded by

reddygmohan6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views37 pages

C++ Program

Uploaded by

reddygmohan6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM 1

Program to find the frequency of the presence of an element in an array:

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

class frequency

private:

int n,m[100],ele,freq;

public:

void getdata();

void findfreq();

void display();

};

Void frequency:: getdata()

Cout<<”Enter the size of the array:”<<endl;

Cin>>n;

cout<<"Enter the number of elements in the array";

for(int i=0;i<n;i++)

cin>>m[i];
cout<<”Enter the search element”;

cin>>ele;

void frequency::findfreq()

Freq=0;

for(int i=0;i<n;i++)

if(ele==m[i])

freq++;

void frequency::display()

if(freq>0)

cout<<”frequency of”<<ele<<”is”<<freq;

else

cout<<ele<<”Does not exist”;

void main()

frequency F;

clrscr();

[Link]();

[Link]();
[Link]();

getch();

}
PROGRAM-2
Program to insert an element in at array at a given position:

#include <iostream.h>
#include <conio.h>
Class insertion
{
Private:
int a[100],n,I,pos,ele;
Public:
void input();
void processing();
void output();
};

Void insertion::input()
{
cout<<”Enter the number of elements in the array”<<endl;
cin<<”Enter the array elements”<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<”Enter the element to be inserted”<<endl;
cin>>ele;
cout<<”Enter the position to be inserted”<<endl;
cin>>pos;
}

Void insertion::processing()
{
If(pos>n| |pos<0)
{
cout<<”invalid position insertion is not possible”<<endl;
getch();
exit(0)
}
else
{
For(i=(n-1);i>=pos;i--)
a[i+1]=a[i];
a[pos]=ele;
n=n+1;
}
}

Void insertion::output()
{
cout<<”the array after insertion”<<endl:
for(i=0;i<n;i++)
cout<<a[i]<<””;
}

Void main()
{
insertion ob;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
PROGRAM-3
Program to delete an element from an array

#include<iostream.h>
#include<conio.h>
Class deletion
{
Private :
int n,a[15],I,pos,ele;
Public:
void input( );
void process( );
void output( );
};

void deletion :: input()


{
cout<<”Enter the number of elements “<<endl;
cin>>n;
cout<<”Enter the array elements “<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<”Enter the position of elements to be deleted “<<endl;
cin>>pos;
}

void deletion :: process()


{
if(pos<0| |pos>=n)
{
cout<<”Invalid position “;
getch( )
exit( 0 );
}
Else
{
ele=a[pos];
for(i=pos;i<n;i++)
{
a[i]=a[i+1];
}
n=n-1;
cout<<”Element “<<ele<<” is successfully deleted”<<endl;
}
}

void deletion :: output( )


{
cout<<”Array after deletion is”<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<”\t”;
}
}

void main ()
{
deletion d;
clrscr( );
[Link]( );
[Link]( );
[Link]( );
getch( );
}
PROGRAM-4
Program to sort the elements of an array using insertion sort:

#include<iostream.h>
#include<conio.h>
class sorting
{
Private:
int n,a[15],I,j,temp;
public:
void input( );
void process( );
void output( );
};

void sorting : : input( )


{
cout<<”Enter number of elements”<<endl;
cin>>n;
cout<<”Enter array elements”<<endl;
for(i=0;i<n;i++)
cin>>a[i];
}

void sorting : : process( )


{
for(i=1;i<n;i++)
{
j=I;
while(j>=1)
{
If(a[j],a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
j=j-1;
}
}
}

void sorting : : output( )


{
cout<<” Array after sorting\n “ ;
for(i=0;i<n;i++)
cout<<a[i]<<” “;
}

void main( )
{
sorting s;
clrscr( );
[Link]( );
[Link]( );
[Link]( );
getch( );
}
PROGRAM-5

Program to search for a given element in an array using binary search


method:

#include<iostream.h>
#include<conio.h>
class search
{
Private:
int n , i , ele , a[15] , low , high , mid , loc ;
public:
void input( );
void binary( );
void output( );
};
void search :: input( )
{
cout<<”Enter the number of elements “<<endl;
cin>>n;
cout<<” Enter the array element:\n “<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<”Enter the element to be searched”<<endl;
cin>>ele;
}
void search :: binary( )
{
loc = -1;
low=0;
high=n – 1;
while(low<=high)
{
mid=(low+high)/2;
if(ele==a[mid])
{
loc=mid;
break;
}
if(ele<a[mid])
high=mid-1;
else
low=mid+1;
}
}
void search :: output( )
{
if(loc>=0)
cout<<” Element “ <<ele<<” is found in position \t “<<loc + 1;
else
cout<<” Element “ <<ele<<” is not found in the array.”;
}
void main( )
{
search b;
clrscr( );
[Link]( );
[Link]( );
[Link]( );
getch( );
}
PROGRAM-6
Program to create a class with data members principle amount , time and
rate of interest. Create member functions to accept data values, to compute
simple interest and to display the result.

#include<iostream.h>
#include<conio.h>
class simple
{
private :
float p,t,r,di;
public :
void input( )
{
cout<<”Enter principle amount, time & rate of interest”<<endl;
cin>>p>>t>>r;
}

void process( )
{
Si=(p*t*r)/100;
}

void output()
{
cout<<”Simple Interest = “<<si;
}
};

void main( )
{
simple s;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
PROGRAM-7

Program to display the roots of a quadratic equation:

#include<iostream.h>
#include<conic.h>
#include<math.h>
class quadratic
{
private:
int a,b,c;
float r1,r2,r,disc;
public:
void input()
{
cout << " input the Co-efficients of the Equation "<<endI;
cin>>a>>b>>c;
}
viod process()
{
disc=(b*b)-(4*a*c);
}
viod output()
{
if( disc ==0 )
{
cout<<"Roots are Real and Equal "<<endI;
R=-B/(2*a);
cout<<"\n The Root = " << r ;
}
else if ( disc > 0 )
{
cout<<"Roots are Real and Distinct"<<endI;
r1=(-b+sqrt(disc))/(2*a);
r2=(-b-sqrt(disc))/(2*a);
cout<<"The First root = " << r1<<endI;
cout<<"The Second root = "<< r2 ;
}
else
cout <<"\n The Roots are imaginary " ;
}
};
void main()
{
quadratic q;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
Program-8

Program to find the area of squares / rectangle / triangle using function


overloading:

#include<iostream.h>
#include<conic.h>
#include<math.h>
Class Shape
{
public :
int area(int side)
{
return(side*side);'
}

float area(double lenght , double breadth)


{
return(lenght*breadth);
}

double area(double s1 , double s2 , double s3)


{
double s=(s1+s2+s3)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
};

void main()
{
int side , ch ;
double lenght , breadth , s1 , s2 , s3 ;
shape obj ;
clrscr();
cout<<"[Link] of Square"<<endl;
cout<<"[Link] of Rectangle"<<endl;
cout<<"[Link] of Triangle"<<endl;
cout<<"Input your choice :"<<endl;
cin>>ch;
if(ch==1)
{
cout<<endl<<"Input Side for the Square"<<endl;
cin>>side;
cout<<"Area of SQUARE = "<< [Link](side);
else if(ch==2)
{
cout<<endl<<"Input Lenght and Breadth for the
Rectangle :"<<endl ;
cin>>lenght>>breadth ;
cout<<"Area of RECTANGLE = "<< [Link](lenght,breadth);
}
else if(ch==3)
{
cout<<endl<<"Input three Sides of Triangle:"<<endl;
cin>>s1>>s2>>s3;
cout<<"Area of TRIANGLE = "<<[Link](s1,s2,s3);
}
else
cout<<endl<<"Invalid Choice " ;
getch() ;
}
Program 9
Write a C++ program to find the cube of a number using inline function:

#include<iostream.h>
#include<conoi.h>
class find cube
{
public:
inline int cube( int a)
{
return(a*a*a);
}
};
void main()
{
findcube F;
int n;
cout<<"Enter a number";
cin>>n;
cout<<"Cube of "<<n<<" is " << [Link](n);
}
Program 10

Program to find the sum of series 1 + X + X2 + X3 + ....... + Xn using


constructors.

#include<iostream.h>
#include<conio.h>
#include<math.h>
{
private :
int x , n , sum , i ;
public :
series()
{
sum=1;
}

void input()
{
cout<<"Input the x and n values";
cin>>x>>n;
}

void calculate()
{
for(i=1;i<=n;i++)
sum=sum+pow(x,i);
}

void output()
{
cout<<"Sum of Series = "<<sum;
}
};
void main()
{
Series S;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
PROGRAM 14
Program to pop the elements of a stack:

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 5
Class stack
{
private:
Int s[max],top,ele,i;
public:
stack()
{
top=-1;
}
void push()
{
If(top==(max-1))
{
cout<<”stack overflow”<<endl;
}
else
{
cout<<”Enter the element to be inserted:”<<endl;
cin>>ele;
top=top+1;
s[top]=ele;
}
}
void pop()
{
If(top==-1)
{
Cout<<”stack underflow”<<endl;
}
else
{
ele=s[top];
Cout<<”The popped element is:”<<ele<<endl;
top=top-1;
}
}
void display()
{
If(top==-1)
{
Cout<<”stack underflow”<<endl;
}
else
{

cout<<”the contents of the stack are “<<endl;

for(i=top;i>=0;i--)

cout<<s[i]<<endl;

};

Void main()

Stack st;

Int choice;

Clrscr();

While(1)
{

cout<<endl<<”--------------------------------------------“<<endl;

cout<<”1:push”<<endl;

cout<<”2:pop”<<endl;

cout<<”3:display”<<endl;

cout<<”4:exist”<<endl;

cout<<”enter your choice”<<endl;

cin>>choice;

switch(choice)

Case1:[Link]();

Break;

Case2:[Link]();

Break;

Case3:[Link]();

Break;

Case4:cout<<”thank you”;

getch();

exit(0);

default:cout<<”invalid input!! Try again”<<endl;

}
Program 15

Program to perform a enqueue and dequeue operations:

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 5
class queue
{
private:
int q[max],front, rear, i, ele;
public:
queue()
{
Front=-1;
rear=-1;
}
void enqueue()
{
If(rear==(max-1))
{

Cout<<”queue is full”<<end l;
}
else
{
If(rear==-1)
{
front=0;
rear=0;
}
else
{
rear =rear+1;
}
cout<<”enter the element to be inserted”<<endl;
cin>>ele;
q[rear]=ele;
}
}
void dequeue()
{
if(front==-1)
{
cout<<”queue is empty”<<endl;
}
else
{
ele=q[font];
cout<<”The element that is removed is”<<ele<<endl;
if(front==rear)

{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
}
}
void display()
{
if(front==-1)
cout<<”the queue is empty”;
else
{
for(i=front;i<=rear;i++)
cout<<q[i]<<” “;
}
}
};
void main()
{
queue Q;
int choice;
clrscr();
while(1)
{
cout<<endl<<”-----------------------------“<<endl;
cout<<”[Link]”<<endl;
cout<<”[Link]”<<endl;
cout<<”[Link]”<<endl;
cout<<[Link]”<<endl;
cout<<”enter your choice”<<endl;
cin>>choice;
switch(choice)
{
case 1:[Link]();
Break;
case 2:[Link]();
Break;
case 3:[Link]();
Break;
case 4:Q cout<<”thank you”<<endl;
getch();
exit(o);
defauit:cout<<”invalid input!! Try again”<<endl;
}
}
}
PROGRAM 16:

Write a program to create a linked list and append nodes:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<process.h>
class linkedlist
{
private:
struct node
{
int info;
node *link;
};
node *newnode,*head,*curnode;
public:
linkedlist()
{
head=null;
}
Void createnode()
{
Newnode=new node;
Cout<<”Enter the information for the new node”<<endl;
Cin>>newnode->info;
Newnode->link=null;
If(head==null)
Head=newnode;
Else
{
Curnode=head;
While((curnode->link)!=null)
{
Curnode=curnode->link;
}
Curnode->link=newnode;
}
}
Void display()
{
Curnode=head;
If(curnode==null)
{
Cout<<”the linked list is empty”<<endl;
}
Else
{

While(curnode->info !=NULL)
{
Cout<<curnode->info<<” “;
Curnode=curnode->link;
}
Cout<<endl;
}
}
};
Void main()
{
Linkedlist I;
Int ch;
Clrscr();
While(1)
{
Cout<<endl<<”1 add node”<<endl;
Cout<<”2 display node”<<endl;
Case<<”3 exit”<<endl;
Cout<<”enter your choice”<<endl;
Cin>>ch;
Switch(ch)
{
Case 1:[Link]();
Break;
Case 2:[Link]();
Break;
Case3:cout<<”thank you”<<endl;
Getch();
Exit(0);
Default:cout<<”invalid input try again”<<endl;
}
}
}

You might also like