Core Practical Programs
Core Practical Programs
1) AIM: Create a C++ program to find volume of sphere using classes and objects
ALGORITHM:
STEP 1-> Start the program
PROGRAM:
#include <iostream>
#include <cmath>
using namespace std;
class Sphere {
private:
double radius;
public:
Sphere(double r) : radius(r) {}
int main() {
double radius;
cout << "Enter the radius of the sphere: ";
cin >> radius;
Sphere sphere(radius);
cout << "Volume of the sphere with radius " << sphere.getRadius() << " is: " <<
sphere.calculateVolume() << endl;
return 0;
}
OUTPUT:
Enter the radius of the sphere: 2.5
Volume of the sphere with radius 2.5 is: 65.4498
_______________________________________________________________________________________________________
2) AIM:Create a C++ program to multiply values, get the values from the user using polymorphism
ALGORITHM:
STEP 1-> Start the program
STEP 4-> Define a class ‘CustomMultiplier’ inherited publicly from ‘Multiplier’ with a
member function
PROGRAM:
#include <iostream>
class Multiplier {
public:
virtual int multiply(int a, int b) const {
return a * b;
}
};
int main() {
Multiplier* multiplier1 = new Multiplier();
Multiplier* multiplier2 = new CustomMultiplier();
OUTPUT:
Enter 2 values : 5 2
Multiplication using base class: 10
Multiplication using derived class: 10
_______________________________________________________________________________________________________
3) AIM:Create a C++ program to display Bike details by using inheritance
ALGORITHM:
STEP 1-> Start the program
STEP 2-> Include the necessary header files ‘iostream’ and ‘string’
PROGRAM:
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
protected:
string brand;
string model;
int year;
public:
Vehicle(string brand, string model, int year)
: brand(brand), model(model), year(year) {}
void displayDetails() {
cout << "Brand: " << brand << endl;
cout << "Model: " << model << endl;
cout << "Year: " << year << endl;
}
};
public:
Bike(string brand, string model, int year, int horsepower)
: Vehicle(brand, model, year), horsepower(horsepower) {}
void displayBikeDetails() {
displayDetails();
cout << "Horse Power: " << horsepower << endl;
}
};
int main() {
Bike myBike("YAMAHA", "RX135", 1995, 14 );
return 0;
}
OUTPUT:
Bike Details:
Brand: YAMAHA
Model: RX135
Year: 1995
Horse Power: 14
_______________________________________________________________________________________________________
ALGORITHM:
STEP 1->Start the program
STEP 2-> Include the necessary header files ‘iostream’ , ‘fstream’ and ‘string’
- use loop to read each line from file using ‘getline()’ until the end of the file is reached.
PROGRAM:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream outputFile("example.txt");
if (outputFile.is_open()) {
outputFile << "Hello, world!" << endl;
outputFile << "This is a C++ program." << endl;
outputFile.close();
cout << "Data has been written to the file." << endl;
} else {
cout << "Error: Unable to open the file for writing." << endl;
}
ifstream inputFile("example.txt");
string line;
if (inputFile.is_open()) {
cout << "Contents of the file:" << endl;
while (getline(inputFile, line)) {
cout << line << endl;
}
inputFile.close();
} else {
cout << "Error: Unable to open the file for reading." << endl;
}
return 0;
}
OUTPUT:
Data has been written to the file.
Contents of the file:
Hello, world!
This is a C++ program.
_______________________________________________________________________________________________________
5)AIM:Create a C++ program to divide values by using exception handling.
ALGORITHM:
STEP 1->Start the program
- divide the numerator and denominator using the exceptional handling concept
PROGRAM:
#include <iostream>
using namespace std;
int main() {
int numerator, denominator;
try {
divide(numerator, denominator);
} catch (const char* message)
{
cout << "Error: " << message << endl;
}
return 0;
}
OUTPUT:
Enter numerator: 45
Enter denominator: 5
Result of division: 9
_______________________________________________________________________________________________________
STEP 4-> Define a class ‘drive’ inherited publicly from ‘base’ with two member functions
- create object ‘obj1’ and ‘obj2’ of classes ‘base’ and ‘derived’ respectively.
- point ‘bptr’ to the object ‘obj1’ and call ‘display()’ and ‘show()’ through ‘bptr’
- point ’bptr’ to the object ‘obj2’ and call ‘display()’ and ‘show()’ through ‘bptr’
PROGRAM:
#include <iostream>
using namespace std;
class base {
public:
void show() {
cout << "\n Base class show:";
}
void display() {
cout << "\n Drive class display:";
}
void show() {
cout << "\n Drive class show:";
}
};
int main() {
base obj1;
base *p;
cout << "\n\t P points to base:\n";
p = &obj1;
p->display();
p->show();
OUTPUT:
P points to base:
P points to drive:
_______________________________________________________________________________________________________
7)AIM:Create a C++ program to make a stack and complete the following given below:
(1)Push “3” values (2)Pop “1” value (3)Display the stack
ALGORITHM:
STEP 1->Start the program
-check if the stack element is empty, if not then print the stack elements
-implement the switch case statement to operate according to the choice made by the
user.
PROGRAM:
#include <iostream>
using namespace std;
int stack[5], n=5, top=-1;
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;
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
break;
}
case 2: {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else
cout<<"The popped element is "<< stack[top] <<endl;
top--;
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
OUTPUT:
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit
Enter choice:
1
Enter value to be pushed:
12
Enter choice:
1
Enter value to be pushed:
14
Enter choice:
1
Enter value to be pushed:
16
Enter choice:
2
The popped element is 16
Enter choice:
3
Stack elements are:14 12
Enter choice:
4
Exit
_______________________________________________________________________________________________________
8)AIM:Create a C++ program to make a queue and satisfy the following given below:
(1)Enqueue “4” values (2)Dequeue “2” values (3)Display the queue
ALGORITHM:
STEP 1->Start the program
-Implement the switch case statement to operate according to the choice made by the
user.
STEP 5->Stop the program.
PROGRAM:
#include <iostream>
#include <queue>
using namespace std;
void display(queue<int>& q) {
if (!q.empty()) {
cout << "Queue elements: ";
queue<int> tempQueue = q;
while (!tempQueue.empty()) {
cout << tempQueue.front() << " ";
tempQueue.pop();
}
cout << endl;
} else {
cout << "Queue is empty." << endl;
}
}
int main() {
cout << "\nQueue Operations:" << endl;
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Display queue"<<endl;
cout << "4. Exit" << endl;
queue<int> myQueue;
int choice, value;
do {
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
myQueue.push(value);
cout << "Value " << value << " enqueued successfully." << endl;
break;
case 2:
if (!myQueue.empty()) {
cout << "Dequeued value: " << myQueue.front() << endl;
myQueue.pop();
} else {
cout << "Queue is empty. No elements to dequeue." << endl;
}
break;
case 3:
display(myQueue);
case 4:
cout << "QUEUE EXITED." << endl;
break;
default:
cout << "Invalid choice. Please enter a valid option." << endl;
}
} while (choice != 4);
return 0;
}
OUTPUT:
Queue Operations:
1. Enqueue
2. Dequeue
3. Display queue
4. Exit
Enter your choice: 1
Enter value to enqueue: 22
Value 22 enqueued successfully.
Enter your choice: 1
Enter value to enqueue: 24
Value 24 enqueued successfully.
Enter your choice: 1
Enter value to enqueue: 26
Value 26 enqueued successfully.
Enter your choice: 1
Enter value to enqueue: 28
Value 28 enqueued successfully.
Enter your choice: 2
Dequeued value: 22
Enter your choice: 2
Dequeued value: 24
Enter your choice: 3
Queue elements: 26 28
Exiting program.
Enter your choice: 4
QUEUE EXITED.
_______________________________________________________________________________________________________
9)AIM:Create a C++ program to sort the values using Bubble Sort & get values from the user.
ALGORITHM:
STEP 1->Start the program
STEP 3->Implement a ‘bubbleSort(int arr[],int n)’ function to sort the array elements
-within the outer loop, start another loop to iterate from the first element to the
-declare another variable ‘arr[n]’ and prompt the user to enter the elements
PROGRAM:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
bubbleSort(arr, n);
return 0;
}
OUTPUT:
_______________________________________________________________________________________________________
10)AIM:Create a C++ program to find the position of the value by implementing binary searching & get
Values from the user.
ALGORITHM:
STEP 1->Start the program
STEP 3->Implement ‘binarySearch(int arr[ ], int n, int largest)’ function to implement the binary
searching.
-check if targeted element is found in the array, if not then print “not found in the array”
PROGRAM:
#include <iostream>
using namespace std;
binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
return -1;
}
int main() {
int n, target;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " sorted integers:" << endl;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
if (index != -1) {
cout << "Element " << target << " found at index " << index << endl;
} else {
cout << "Element " << target << " not found in the array." << endl;
}
return 0;
}
OUTPUT:
Enter the number of elements in the array: 5
Enter 5 sorted integers:
22 44 66 88 99
Enter the target element to search: 88
Element 88 found at index 3
_______________________________________________________________________________________________________
ALGORITHM:
STEP 1->Start the program
STEP 3->Define a structure `node` to represent a node in a binary tree. Each node contains an
integer data value and pointers to its left and right child nodes.
STEP 4->Implement a function `createnode` to create a new node with the given data:
`NULL`.
STEP 5-> Implement a recursive function `inorder` to perform an inorder traversal of the binary
tree:
-Create a binary tree with some nodes and set their values.
-Call the `inorder` function with the root node to perform inorder traversal.
PROGRAM:
#include <iostream>
using namespace std;
struct node {
int data;
node* left;
node* right;
};
node *createnode(int data)
{
node *newnode =new node();
if(!newnode)
{
cout<<"memory error \n";
return NULL;
}
newnode->data=data;
newnode->left=newnode->right =NULL;
return newnode;
}
void inorder(node* temp) {
if (temp == NULL)
return;
inorder(temp->left);
cout << temp->data << " ";
inorder(temp->right);
}
int main() {
node* root = createnode(11);
root->left = createnode(33);
root->right = createnode(55);
root->left->left = createnode(77);
root->left->right = createnode(99);
return 0;
}
OUTPUT:
Inorder Traversal: 77 33 99 11 55
_______________________________________________________________________________________________________
12)AIM:Create a C++ program to make a Singly Linked List using Malloc function.
ALGORITHM:
STEP 1->Start the program.
STEP 3->Define a struct `node` to represent a node in a linked list. Each node contains an
STEP 4->Declare a global pointer `head` initialised to `NULL` to point to the first node of the
linked list.
STEP 5->Implement the `append` function to insert a new node with given data at the
-Set the `next` pointer of the new node to point to the current `head`.
-Update the `head` pointer to point to the new cell.
STEP 6->Implement the `show` function to display the elements of the linked list:
-Initialise a pointer `ptr` to traverse the linked list, starting from the `head`.
-While `ptr` is not `NULL`, print the data value of the current node and move `ptr` to
-Insert several elements into the linked list using the `insert` function.
-Call the `show function to print the elements of the linked list.
PROGRAM:
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node* head =NULL;
void append(int value)
{
struct node* new_cell =(struct node*)
malloc (sizeof(struct node));
new_cell->data = value;
new_cell->next = head;
head = new_cell;
}
void show()
{
struct node* ptr;
ptr = head;
while(ptr != NULL)
{
cout<<ptr->data<<" --> ";
ptr=ptr->next;
}
}
int main()
{
append(11);
append(22);
append(33);
append(44);
cout<<"THE CREATED SINGLY LINKED LIST IS : \n";
show();
return 0;
}
OUTPUT:
THE CREATED SINGLY LINKED LIST IS :
44 --> 33 --> 22 --> 11 -->
*****************************************************************************************************