C++ Practical Program (1-15)
C++ Practical Program (1-15)
C++ Practical Program (1-15)
************************************************************************
Program:
#include<iostream.h>
#include<conio.h>
class prime
{
int a,k,i;
public:
prime (int x)
{
a=x;
}
void calculate()
{
k=1;
{
for(i=2;i<=a/2;i++)
if(a%i==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<<”\n”<<a<<”is a prime number”;
else
cout<<”\n”<<a<<”is not a prime number”;
}
};
int main()
{
int a;
cout<<”enter the number:”;
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
getch();
return 0;
}
Output (Prime number)
*****************************
Enter the number: 11
11 is a prime number
Enter the number: 10
10 is not a prime number
2. Program to implement copy constructor.
*************************************
Program:
#include<iostream.h>
#include<conio.h>
class Demo
{
private:
int num1, num2;
public:
Demo(int n1, int n2)
{
num1 = n1;
num2 = n2;
}
Demo(const Demo &n)
{
num1 = n.num1;
num2 = n.num2;
}
void display()
{
cout<<"num1 = "<< num1 <<endl;
cout<<"num2 = "<< num2 <<endl;
}
};
int main()
{
Demo obj1(10, 20);
Demo obj2 = obj1;
obj1.display();
obj2.display();
return 0;
}
Output (Copy constructor)
***********************
num1 = 10
num2 = 20
num1 = 10
num2 = 20
3.Program using Inline function to find factorial of a given no.
*****************************************************
Program:
#include <iostream.h>
#include <conio.h>
inline void factorial (int x)
{
int i,f=1,p=1;
for(i=1;i<=x;i++)
{
f=f*i;
}
cout<<"Factorial of "<<x<<" is "<<f<<endl;
}
int main( )
{
int i, N;
cout<<"C++ Program to print factorial of first N natural
numbers"<<endl<<endl;
cout<<"Enter total number of natural numbers:"<<endl;
cin>>N;
for(i=1;i<=N;i++)
{
factorial(i);
}
getch();
return 0;
}
Output(Factorial using Inline function)
**********************************
C++ Program to print factorial of first N natural numbers
Enter total number of natural numbers:
6
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
Factorial of 6 is 720
4.Program to Implement Operator Overloading.
*****************************************
Program:
#include<iostream.h>
#include<conio.h>
class temp
{
private:
int data;
public:
void getvalue()
{
cin>>data;
}
temp operator+(temp ob)
{
temp t;
t.data=data + ob.data;
return t;
}
temp operator- (temp ob)
{
temp t;
t.data = data - ob.data;
return t;
}
int display()
{
return data;
}
};
int main()
{
temp obj1, obj2, sum, sub;
cout<<"Enter an integer value for obj1: ";
obj1.getvalue();
cout<<"Enter an integer value for obj2: ";
obj2.getvalue();
sum= obj1+obj2;
sub=obj1-obj2;
cout<<"Addition result is = "<<sum.display()<<endl;
cout<<"Subtraction result is = "<<sub.display()<<endl;
getch();
}
Output (Operator Overloading)
***************************
Enter an integer value for obj1:20
Enter an integer value for obj2:10
Addition result is=30
Subtraction result is =10
5 . Program to implement Multiple Inheritance.
*****************************************
Program:
#include <iostream.h>
#include<conio.h>
class A
{
public:
int x;
void getx()
{
cout << "enter value of x: "; cin >> x;
}
};
class B
{
public:
int y;
void gety()
{
cout << "enter value of y: "; cin >> y;
}
};
class C : public A, public B
{
public:
void sum()
{
cout << "Sum = " << x + y;
}
};
int main()
{
C obj1;
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
}
Output(Multiple Inheritance)
**************************
enter value of x: 5
enter value of y: 4
Sum = 9
6 . Program to implement Push, Pop Operations of Stack using Arrays.
*************************************************************
Program:
#include <iostream>
#include<conio.h>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main()
{
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do
{
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
cout<<"Exit"<<endl;
break;
}
default:
{
cout<<"Invalid Choice"<<endl;
}
}
} while(ch!=4);
return 0;
}
Output(Push,Pop Operation using Array)
************************************
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit
Enter choice: 1
Enter value to be pushed: 2
Enter choice: 1
Enter value to be pushed: 6
Enter choice: 1
Enter value to be pushed: 8
Enter choice: 1
Enter value to be pushed: 7
Enter choice: 2
The popped element is 7
Enter choice: 3
Stack elements are:8 6 2
Enter choice: 5
Invalid Choice
Enter choice: 4
Exit
7. Program to implement Push, Pop Operations of Stack using Pointers.
*************************************************************
Program:
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void display();
};
void stack::push()
{
node *newlink;
newlink=new node;
cout<<"enter the pointer element to push"<<endl;
cin>>newlink->data;
newlink->next=top;
top=newlink;
cout<<"push operation is successful";
}
void stack::pop()
{
if(top==NULL)
cout<<"stack is empty";
else
{
node *t;
t=top;
top=top->next;
cout<<"popped the elements"<<t->data;
delete t;
cout<<"\n pop operation is successful";
}
}
void stack::display()
{
node *curt=top;
if(top==NULL)
cout<<"stack empty";
else
{
cout<<"the elements in the stack is\n";
while(curt!= NULL)
{
cout<<curt->data;
curt = curt->next;
}
}
}
void main()
{
stack s1;
int ch=0;
clrscr();
cout<<"stack operation using pointer";
cout<<"\n 1.PUSH \n 2.POP \n 3.DISPLAY \n4.EXIT\n";
while(ch!=4)
{
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
s1.push();
break;
case 2:
s1.pop();
break;
case 3:
s1.display();
break;
case 4:
cout<<"program terminated";
break;
}
}
getch();
}
Output (Push,Pop Operation using Pointer)
**************************************
STACK OPERATION USING POINTER
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the choice: 1
enter the pointer element to push: 20
push operation is successful.
Enter the choice: 1
enter the pointer element to push: 30
push operation is successful
struct queue
{
q1 q;
node *t;
public:
void initialize()
{
q.front = NULL;
q.rear = NULL;
}
void addq();
void delq();
void displayq();
};
void queue:: addq()
{
t = new(node);
cout << "Enter Item To Add:";
cin >> t->data;
cout << "Item Added is:" << t->data;
t->link = NULL;
if((q.rear)== NULL)
q.front = t;
else
q.rear->link = t;
q.rear = t;
}
void queue::delq()
{
if(q.front == NULL)
{
cout << "Queue Is Empty";
q.rear = NULL;
}
else
{
t = q.front;
cout << "Item Deleted Is:" << q.front->data;
q.front=q.front->link;
free(t);
}
}
void queue::displayq()
{
if(q.front == NULL)
{
cout << " Queue Is Empty";
}
else
{
cout << "\nFRONT";
for(t=q.front;t!=NULL;t=t->link)
cout << "-->" << t->data;
cout << "<--REAR\n";
}
}
void main()
{
int choice;
queue qu;
clrscr();
qu.initialize();
cout <<"\n\t1-ADD";
cout <<"\n\t2-DELETE";
cout <<"\n\t3-DISPLAY";
cout <<"\n\t4-EXIT";
while(choice != 4)
{
cout << "\nEnter Your Choice:\t";
cin >> choice;
switch(choice)
{
case 1:
qu.addq();
break;
case 2:
qu.delq();
break;
case 3:
qu.displayq();
break;
case 4:
break;
default:
cout << "Invalid Choice";
}
}
FRONT-->44-->55<--REAR
FRONT-->55<--REAR
Second polynomial is
1+2x^1+4x^2+8x^3
Sum of polynomial is
6+2x^1+14x^2+14x^3
13. CREATION, INSERTION AND DELETION IN DOUBLY LINKED
****************************************************************
LIST
*****
PROGRAM:
#include<iostream.h>
#include<conio.h>
struct node
{
node*left;
node*right;
int data;
}*p;
node *head,*temp,*perm,*temp1,*ins;
char ans;
int an1,da1,cho,flag;
class dll
{
public:
void display(node *head);
void create();
void insert();
void dele();
};
void dll::create()
{
head=new node;
head->left=NULL;
cout<<”\n\t\t\t enter the data:”;
cin>>head->data;
head->right=NULL;
}
void dll::insert()
{
flag=0;
cout<<”\n\t\t\t before which element to insert(press 0 to add at end):”;
cin>>an1;
cout<<an1;
ins=new node;
cout<<”\n\t\t\t insert the data:”;
cin>>ins->data;
cout<<ins->data;
ins->right=NULL;
if(head->data==an1)
{
ins->right=head;
head->left=ins;
head=ins;
}
else
if(an1==0)
{
temp=head;
while(temp!=NULL)
{
perm=temp;
temp=temp->right;
}
perm->right=ins;
ins->left=perm;
}
else
{
temp=head;
while(temp!=NULL)
{
flag=1;
break;
}
}
if(flag==1)
perm->right=ins;
ins->left=perm;
temp->left=ins;
}
cout<<”\n\t\t\t element not found”;
}
void dll::dele()
{
flag=0;
if (head==NULL)
{
cout”\n\t\t\t list is empty so unable to delete:”;
return:
}
else
{
cout<<”\n\t\t\t which to delete:”;
cin>>da1;
cout<<da1;
if(head->data==da1)
head=head->right
else
temp=head;
while(temp->data==da1)
{
perm=temp;
temp=temp->right;
if(temp->data==da1)
{
flag=1;
break;
}
}
if(flag==1)
{
temp1=temp->right;
perm->right=temp1;
}
else
cout<<”\n\t\t\t element not found”;
}
}
}
void dll::display(node *head)
{
temp=head;
cout<<”\n\t\t\t doubly linked list look as follows:”;
cout<<”\n\t\t\t”;
while(temp!=NULL)
{
cout<<temp->data<<”<-->”;
temp=temp->right;
}
}
void main()
{
dll d;
int choice;
clrscr();
cout<<”\n\t\t\t creation, insertion and deletion in doubly linked list”;
cout<<”\n\t\t\t ………………………………………………………….”;
do
{
cout<<”\n\t\t\t 1-CREATION”;
cout<<”\n\t\t\t 2-INSERTION”;
cout<<”\n\t\t\t 3-DELETION”;
cout<<”\n\t\t\t 4-EXIT”;
cout<<”\n\t\t\t ENTER YOUR CHOICE:”;
cin>>choice;
cout<<choice;
switch(choice)
{
case 1:d. create();
d.display(head);
break;
case 2:d. insert();
d.display(head);
break;
case 3:d. dele();
d.display(head);
break;
case 4:break;
}
}
while(choice!=4);
getch();
}
OUTPUT (CREATION, INSERTION, AND DELETION IN DLL)
**********************************************************
1-CREATION
2-INSERTION
3-DELETION
4-EXIT
ENTER YOUR CHOICE: 1
ENTER THE DATA: 10
DOUBLY LINKED LIST LOOK AS FOLLOWS:
10<--> NULL
1-CREATION
2-INSERTION
3-DELETION
4-EXIT
ENTER YPOUR CHOICE: 2
BEFORE WHICH ELEMENT TO INSERT (PRESS 0 TO ADD AT END): 0
INSERT THE DATA: 20
DOUBLY LINKED LIST LOOK AS FOLLOWS:
10<-->20< -->NULL
1-CREATION
2-INSERTION
3-DELETION
4-EXIT
ENTER YOUR CHOICE:2
BEFORE WHICH ELEMENT TO INSERT ( PRESS 0 TO ADD AT END ):
10
INSERT THE DATA:5
DOUBLY LINKED LIST LOOK AS FOLLOWS:
5< -- >10< -->20< -- >NULL
1-CREATION
2-INSERTION
3-DELETION
4-EXIT
ENTER YOUR CHOICE: 2
BEFORE WHICH ELEMENT TO INSERT (PRESS 0 TO ADD AT END): 20
INSERT THE DATA: 15
DOUBLY LINKED LIST LOOK AS FOLLOWS:
5< -- >10<-->15<-->20<-->NULL
14. BINARY TREE TRAVERSALS (IN-ORDER, PRE-ORDER,
********************************************************
POST-ORDER) USING LINKED LISTS.
************************************
PROGRAM:
#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
struct Node *left, *right;
Node (int data)
{
this->data=data;
left=right=NULL;
}
};
void Postorder(struct Node* node)
{
if(node==NULL)
return;
Postorder(node->left);
Postorder(node->right);
cout<<node->data<<” “ ;
}
void Inorder(struct Node* node)
{
if(node==NULL)
return;
Inorder( node->left);
cout<<node->data<< “ “ ;
Inorder(node->right);
}
void Preorder(struct Node* node)
{
if (node==NULL)
return;
cout<<node->data<<” “ ;
Preorder(node->left);
Preorder(node->right);
}
int main()
{
struct Node* root=new Node(1);
root->left=new Node(2);
root->right=new Node(3);
root->left->left=new Node(4);
root->left->right=new Node(5);
cout<<”\n Preorder traversal of binary tree is \n “ ;
Preorder(root);
cout<<”\n Inorder traversal of binary tree is \n” ;
Inorder(root);
cout<<”\n Postorder traversal of binary tree is \n”;
Postorder(root);
return 0;
}
OUTPUT: (BINARY TREE TRAVERSALS)
****************************************
Preorder traversal of binary tree is
12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231
15 a. Program to implement Depth First Search for Graph using Recursion
****************************************************************
PROGRAM:
#include<iostream.h>
class graph
{
int a[10][10],n,start;
public:
void getdata();
void dfs_traverse();
};
void graph::getdata()
{
cout<<"Enter the number of vertices in the graph ";
cin>>n;
cout<<"Enter the adjacency matrix of graph "<<endl;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
cout<<"Enter the vertex from which you want to traverse ";
cin>>start;
}
void graph::dfs_traverse()
{
int *visited= new int[n];
int stack[10],top=-1,i;
for(int j=0;j<n;j++)
visited[j]=0;
cout<<"The Depth First Search Traversal : "<<endl;
i=stack[++top]=start;
visited[start]=1;
while(top>=0)
{
i=stack[top];
cout<<stack[top--]<<endl;
for(int j=n-1;j>=0;j--)
if(a[i][j]!=0&&visited[j]!=1)
{
stack[++top]=j;
visited[j]=1;
}
}
}
int main()
{
graph dfs;
dfs.getdata();
dfs.dfs_traverse();
return 0;
}
OUTPUT (DEPTH FIRST SEARCH)
********************************
Enter the number of vertices in the graph 5
Enter the adjacency matrix of graph
01100
10010
10011
01100
00110
Enter the vertex from which you want to traverse 0
The depth first search traversal:
0
1
3
2
4
Process returned 0 (0*0) execution time:26.410s
15 b.Program to implement Breadth First Search for Graph using
********************************************************
Recursion
*********
PROGRAM:
#include<iostream>
class graph
{
int a[10][10],n,start;
public:
void getdata();
void bfs_traverse();
};
void graph::getdata()
{
cout<<"Enter the number of vertices in the graph ";
cin>>n;
cout<<"Enter the adjacency matrix of graph "<<endl;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
cout<<"Enter the vertex from which you want to traverse ";
cin>>start;
}
void graph::bfs_traverse()
{
int *visited= new int[n];
int queue[10],front=-1,rear=0,i;
for(int j=0;j<n;j++)
visited[j]=0;
cout<<"Traversing the graph using breadth first search algorithm : "<<endl;
queue[rear]=start;
visited[start]=1;
while(front!=rear)
{
cout<<queue[++front]<<endl;
i=queue[front];
for(int j=0;j<n;j++)
if(a[i][j]!=0&&visited[j]!=1)
{
queue[++rear]=j;
visited[j]=1;
}
}
}
int main()
{
graph bfs;
bfs.getdata();
bfs.bfs_traverse();
return 0;
}
OUTPUT: