Professional Documents
Culture Documents
Subject code:BCS18L02
Regulation: 2022-2023
RECORD NOTEBOOK
2022-2023(ODD SEMESTER)
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
Certified that this is the bonafide record of work done by YADAVA THARUN
of II Year.B.Tech.[CSE DS(AI),Sec-C in the OBJECT ORIENTED PROGRAMMING
WITH C++ LAB during the year 2022-2023.
DATE: - 11/08/2022
AIM:
To find whether a given number is prime or not
ALGORITHM:
1. Start the program
2. Initialize the variables
3. This program takes a positive integer from user and stores it in variable n.
4. Then, for loop is executed which checks whether the number entered by user
is perfectly divisible by i or not.
5. The for loop initiates with an initial value of i equals to 2 and increasing the value
of i in each iteration.
6. If the number entered by user is perfectly divisible by i then, isPrime is set to false
and the number will not be a prime number.
7. But, if the number is not perfectly divisible by i until test condition i<=n/2 is true
means, it is only divisible by 1 and that number itself. So, the given number is a prime
number.
1
PROGRAM
// To find whether a given number is prime or not
#include <iostream.h>
#include <conio.h>
int main()
{
int n, i;
bool isPrime = true;
clrscr();
cout << "Enter a positive integer: ";
cin >> n;
for(i = 2; i <= n / 2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout << "This is a prime number";
else
cout << "This is not a prime number";
getch();
return 0;
}
2
OUTPUT
RESULT:
Thus the C++ program for checking prime number was verified and executed
successfull
3
EX NO: 1B PROGRAM TO IMPLEMENT SWITCH-CASE STATEMENT AND DO
WHILE
DATE: - 11/08/2022
AIM :
To implement the switch case statement and do while in C++
ALGORITHM:
4
PROGRAM:
// Program to implement the switch case statement and do while
#include <iostream.h>
#include <conio.h>
int main()
{
const double ADULT_RATE = 40.0;
const double CHILD_RATE = 20.0;
const double SENIOR_RATE = 30.0;
int choice; // Menu choice
int months; // Number of months
double charges; // Monthly charges
clrscr();
do
{
// Display the menu and get the user's choice
cout << "\n Health Club Membership Menu\n\n";
cout << "1. Standard Adult Membership\n";
cout << "2. Child Membership\n";
cout << "3. Senior Citizen Membership\n";
cout << "4. Quit the Program\n\n";
cout << "Enter your choice: ";
cin >> choice;
// Validate the menu selection
while ((choice < 1) || (choice > 4))
{
cout << "Please enter 1, 2, 3, or 4: ";
cin >> choice;
}
// Process the user's choice
if (choice != 4)
{
cout << "For how many months? ";
cin >> months;
// Compute charges based on user input
switch (choice)
{
case 1:
charges = months * ADULT_RATE;
break;
case 2:
charges = months * CHILD_RATE;
break;
case 3:
charges = months * SENIOR_RATE;
}
// Display the monthly charges
cout << fixed << showpoint << setprecision(2);
cout << "The total charges are $" << charges << endl;
}
5
} while (choice != 4); // Loop again if the user did not
// select choice 4 to quit
getch();
return 0;
}
6
OUTPUT:
RESULT:
Thus the C++ program to implement the switch case statement and do while was verified and
executed successfully
7
EX NO: 1C DISPLAY THE SQUARE OF NUMBERS FROM1 TO 5 USING FOR
LOOP
DATE: - 25/08/2022
AIM:
To display the square of numbers from1 to 5 using for loop
ALGORITHM:
1. Start the program
2.Initialize the variables
3. Initialise the for loop to find the square of numbers
4. Stop the program
8
PROGRAM:
// This program uses a for loop to display the numbers 1-5 and their squares .
#include
<iostream.h>
#include <conio.h>
int main()
{
int nr;
clrscr();
cout << "Number
Squared\n"; cout << " \
n";
for (nr = 1; nr <= 5; nr++)
cout << setw(4) << nr << setw(7) << (nr * nr) <<
endl; getch();
return 0;
}
9
OUTPUT
RESULT:
Thus the C++ program to display the square of numbers from1 to 5 using for loop
was verified and executed successfully
10
EX NO: 1D EVEN NUMBER CHECKING USING WHILE LOOP
DATE: - 25/08/2022
AIM:
To print all the even numbers from 1 to certain number entered by user using while loop
ALGORITHM:
1. Start the program
2. Initialise the variables
3. Enter the number of numbers
4. Check the numbers whether it is even or not using if statement
5. If n is divisible by 2 then it is an even number
6. Repeat until it reaches the number of numbers
11
PROGRAM:
// To print all the even numbers from 1 to certain number entered by user using while
loop
#include <iostream.h>
#include <conio.h>
int main()
{
int n,i=1;
clrscr();
cout <<"Enter a number:";
cin>>n;
while (i <= n)
{
if (i % 2 == 0)
cout<<i<<endl;
i++;
}
getch();
return 0;
}
12
OUTPUT:
RESULT:
Thus the C++ program To print all the even numbers from 1 to certain number
entered by user using while loop was verified and executed successfully.
13
EX NO: 2A TO PRINT THE BOOK DETAILS USING STRUCTURE
DATE: - 01/09/2022
AIM:
ALGORITHM:
14
PROGRAM:
// To print the book details using structure
include
<iostream.h>
#include
<cstring.h>
#include <conio.h>
struct Books
{ char
title[50];
char author[50];
char
subject[100]; int
book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type
Book struct Books Book2; // Declare Book2 of
type Book clrscr();
// book 1 specification
strcpy( Book1.title, "Learn C++
Programming"); strcpy( Book1.author,
"Chand Miyan");
strcpy( Book1.subject, "C++
Programming"); Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom
Billing"); strcpy( Book2.author,
"Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// Print Book1 info
cout << "Book 1 title : " << Book1.title <<endl;
cout << "Book 1 author : " << Book1.author
<<endl; cout << "Book 1 subject : " <<
Book1.subject <<endl; cout << "Book 1 id : "
<< Book1.book_id <<endl;
// Print Book2 info
cout << "Book 2 title : " << Book2.title <<endl;
cout << "Book 2 author : " << Book2.author
<<endl; cout << "Book 2 subject : " <<
Book2.subject <<endl; cout << "Book 2 id : "
<< Book2.book_id <<endl; getch();
return 0;
15
OUTPUT:
RESULT:
Thus the C++ program to print the book details using structure was verified and
executed successfully
16
EX NO: 2B TO PRINT THE EMPLOYEE DETAILS USING UNION
DATE: - 01/09/2022
AIM
To display the employee details using union
ALGORITHM
1. Start the program
2. Give the employee details in union
3. Declare the union variables
4. Display the employee details using union object and union member
5. Stop the program
17
PROGRAM:
// To display the employee details using union
#include <iostream.h>
#include <conio.h>
union Emp
{
int num;
double sal;
};
int main()
{
Emp value;
clrscr();
value.num = 2;
cout << "Employee Number::" << value.num<< "\nSalary is:: " << value.sal << endl;
value.sal = 2000.0;
cout << "Employee Number::" << value.num<< "\nSalary is:: " << value.sal << endl;
getch();
return 0;
}
18
OUTPUT :
RESULT:
Thus the C++ program to print the employee details using union was verified and
executed successfully
19
EX NO: 3 PROGRAM FOR POINTER ARITHMETIC
DATE: - 04/09/2022
AIM:
ALGORITHM:
1. Start the program
2. Initialise the variables
3. Initialise the pointer
4. Using for loop print the address and value of the data
5. Stop the program
20
PROGRAM
// To implement the concept pointer arithmetic in C++//
#include <iostream.h>
#include <conio.h>
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
clrscr();
// let us have array address in pointer.
ptr = var;
for (int i = 0; i < MAX; i++) {
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// point to the next location
ptr++;
}
getch();
return 0;
}
21
OUTPUT:
RESULT:
Thus the C++ program to implement the concept pointer arithmetic
was verified and executed successfully
22
EX NO: 4a PROGRAM TO FIND THE SMALLEST NUMBER USING FUNCTION
DATE: - 04/09/2022
AIM:
To find the smallest number using function in C++
ALGORITHM:
1. Start the program
2. Declare the function
3. Define the function to compare two numbers
4. Create a function call to execute
5. Stop the program
23
PROGRAM:
// Simple Example Program Of Function Find Smallest Number //
#include <iostream.h>
#include <conio.h>
// Find Smallest Number compare Function
int compare(int a, int b) {
return (a < b) ? a : b;
}
int main() {
clrscr();
cout << "\nSmallest Number :" << compare(1, 10);
cout << "\nSmallest Number :" << compare(31, 10);
cout << "\nSmallest Number :" << compare(11, 8);
getch();
return 0;
}
24
OUTPUT
RESULT:
Thus the C++ program to find the smallest number using function
was verified and executed successfully
25
EX NO: 4b PROGRAM TO FIND THE FACTORIAL OF A NUMBER USING
RECURSION
DATE: - 15/09/2022
AIM:
To find the factorial of a number using recursion in C++
ALGORITHM:
1. Start the program
2. Get the input value n
3. Create a function call for finding the factorial f a given
number 3.If n = 0 ,then declare that n! = 1.
4. Otherwise, n must be positive. Solve the sub problem of computing (n−1)!, multiply
this result by n, and declare n! equal to the result of this product.
5. Stop the program
26
PROGRAM:
/* Program For Factorial Value Using Recursion In C++*/
#include <iostream.h>
#include <conio.h>
//Function Declaration
long factorial(int);
int main() {
// Variable Declaration
int counter, n;
clrscr();
// Get Input Value
cout << "Enter the Number :";
cin >> n;
// Factorial Function Call
cout << n << " Factorial Value Is " << factorial(n);
// Wait For Output Screen
getch();
return 0;
}
// Factorial recursion Function
long factorial(int n) {
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
27
Output:
RESULT:
Thus the C++ program to find the factorial of a number using recursion was verified
and executed successful
28
EX NO: 5 PROGRAM TO FIND THE CUBE OF A NUMBER USING INLINE
FUNCTION
DATE: - 15/09/2022
AIM:
ALGORITHM:
29
PROGRAM:
// To find the cube of a number using inline function
#include <iostream.h>
#include <conio.h>
30
OUTPUT:
RESULT:
Thus the C++ program to find the cube of a number using inline function was
verified and executed successfully
31
EX NO: 6A PROGRAM TO SWAP TWO NUMBERS USING CALL BY VALUE.
DATE: - 22/09/2022
AIM:
To write a C++ program to swap two numbers using call by value.
ALGORITHM:
1. Start the program.
2. Set a ← 100 and b ← 200
3. Call the function swap(a,b)
4. Start function.
5. Assign temp ← a
6. Assign a ← b
7. Assign b ← temp
8. Print a and b.
9. End function.
10. Stop the program
32
PROGRAM:
// To swap two numbers using call by value
#include <iostream.h>
#include <conio.h>
void main()
{
int a=100, b=200;
clrscr();
swap(a, b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}
33
OUTPUT
RESULT:
Thus the C++ program to swap two numbers using call by value was verified and
executed successfully
34
EX NO: 6B PROGRAM TO SWAP TWO NUMBERS USING CALL BY
REFERENCE.
DATE: - 22/09/2022
AIM :To write a C program to swap two numbers using call by reference.
ALGORITHM:
35
PROGRAM
// To swap two numbers using call by reference.
#include <iostream.h>
#include <conio.h>
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int a=100, b=200;
clrscr();
swap(&a, &b); // passing address to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}
36
OUTPUT
RESULT:
Thus the C++ program to swap two numbers using call by reference was verified and
executed successfully
37
EX NO: 7 PROGRAM TO CREATE AUTOMATIC, GLOBAL, STATIC
AND REGISTER VARIABLES
DATE: - 06/10/2022
AIM:
To write a C++ program to create automatic, global, static and register variables.
ALGORITHM
1. Start the program
2. Declare , g as a global variable, s as static, r as register and a as
automatic variable.
3. Define two function, first is main() and another is test_function().
4. Since g is global variable, it can be used in both function.
5. Variables r and s are declared inside test_function() so can
only be used inside that function
6. When test_function() is called for the first time, r is initialized to
5 and the value of s is 10 which is calculated from the
statement,s=s+r*2;
7. After the termination of test_function(), r is destroyed but s still
holds 10.
8. When it is called second time, r is created and initialized to 5
again. Now, the value of s becomes 20 since s initially held 10.
9. Variable a is declared inside main() and can only be used
inside main().
10. Print the output
11. Stop the program
38
PROGRAM:
// To create automatic, global, static and register variables.
#include <iostream.h>
#include <conio.h>
int g; //global variable, initially holds 0
void test_function()
{
static int s; //static variable, initially holds 0
register int r; //register variable
r=5;
s=s+r*2;
cout<<"Inside test_function"<<endl;
cout<<"g = "<<g<<endl;
cout<<"s = "<<s<<endl;
cout<<"r = "<<r<<endl;
}
int main()
{
int a; //automatic variable
clrscr();
g=25;
a=17;
test_function();
cout<<"Inside main"<<endl;
cout<<"a = "<<a<<endl;
cout<<"g = "<<g<<endl;
test_function();
getch();
return 0;
}
39
OUTPUT
RESULT:
Thus the C++ program to create automatic, global, static and register variables was
verified and executed successfully
40
EX NO: 8 PROGRAM TO ADD TWO NUMBERS USING CONSTRUCTOR AND
DESTRUCTOR
DATE: - 06/10/2022
AIM:
To write a C++ program to add two numbers using constructor and destructor.
ALGORITHM:
41
PROGRAM:
// To add two numbers using constructor and destructor.
#include <iostream.h>
#include <conio.h>
class add
{
int c;
public:
add(int a, int b); //constructor function
void display();
~add() //destructor function
{
cout<<"Objects are destroyed"<<endl;
}
};
add::add(int a, int b)
{
c=a+b;
}
void add:: display()
{
cout<<"The addition of 2 number is"<<c;
}
void main()
{
int c,d; clrscr();
clrscr();
cout<<"Enter the value of c and d"<<"\n";
cin>>c>>d;
add a1(c,d);
a1.display();
getch();
}
42
OUTPUT:
RESULT:
Thus the C++ program to add two numbers using constructor and destructor was
verified and executed successfully
43
EX NO: 9 PROGRAM TO DISPLAY THE VALUE USING THIS POINTER
DATE: - 20/10/2022
AIM
To display the value using this pointer in C++
ALGORITHM
1. Start the program
2. Get the input value
3. 'a' is a private member of the class This_Pointer.
4. Also, the arguments received by the member function printData() is also a.
5. Hence, if we do not use this pointer explicitly both the 'a' will be considered
as data members.
6. Print the data
7. End the program
44
PROGRAM:
// To display the value using this pointer
#include <iostream.h>
#include <conio.h>
class
This_Pointer{ int
a;
public:
void setData(int a){
this->a = a;
}
void printData(){
cout<<"The value of a is"<<a<<endl;
}
};
int main()
{ This_Pointer
tp; clrscr();
tp.setData(10);
tp.printData();
getch();
return 0;
45
OUTPUT:-
RESULT:
Thus the C++ program to display the value of this pointer was verified and executed
successfully
46
EX NO: 10A PROGRAM TO IMPLEMENT MULTIPLE INHERITANCES –
ACCESS SPECIFIERS
DATE: - 20/10/2022
AIM
To implement multiple inheritances –access specifiers
ALGORITHM
47
PROGRAM:
// To implement multiple inheritances –access specifiers
#include <iostream.h>
#include <conio.h>
class base1
{
protected:
int z;
private:
int y;
public:
int x;
void set_value(int xx,int yy, int zz)
{
x=xx;
y=yy;
z=zz;
}
void display1()
{
cout<<"\nX="<<x;
cout<<"\ny="<<y;
cout<<"\nZ="<<z;
}
};
class base2
{
protected:
int a;
private:
int b;
public:
int c;
void set_values(int aa,int bb, int cc)
{
a=aa;
b=bb;
c=cc;
}
void display2()
{
cout<<"\nA="<<a;
cout<<"\nB="<<b;
}
};
class derived:public base1,private base2
{
public:
void display()
{
a=100;
c=200;
cout<<"\nValue of X in derived class="<<x;
//cout<<"\nValue of Y in derived class="<<y; base1::y not accessible cout<<"\
nValue of Z in derived class="<<z;
cout<<"\nValue of A in derived class="<<a;
//cout<<"\nValue of B in derived class="<<b;base2::b not accessible cout<<"\
nValue of C in derived class="<<c;
}
};
48
OUTPUT:
RESULT:
Thus the C++ program to implement multiple inheritances –access specifiers was
49
EX NO: 10B HIERARCHICAL INHERITANCE – FUNCTION OVERR IDING
/VIRTUAL FUNCTION
DATE: - 27/10/2022
AIM
To implement Hierarchical inheritance – Function Overriding /Virtual Function
ALGORITHM
1. Start the program
2. Create a class polygon with member function get_data() and float area()
3. Create a class rectangle with member function float area()
4. Create a class triangle with member function float area()
5. In the main function create objects of respective class
6. Access the member function with the help of objects
7. End the program
50
PROGRAM:
// Hierarchical inheritance – Function Overriding /Virtual Function
#include <iostream.h>
#include <conio.h>
class c_polygon
{
protected:
float a,b;
public:
void get_data()
{
cout<<"\nEnter any two floating values:\n";
cin>>a>>b;
}
virtual float area()
{
return 0;
}
};
public:
float area()
{
return (b*a/2);
}
};
void main()
{
clrscr();
c_rectangle r;
c_triangle t;
c_polygon *p;
p=&r;
p->get_data();
cout<<"\nArea of rectangle is "<<p->area();
p=&t;
p- >get_data();
cout<<"\nArea of triangle is "<<p->area();
getch();
}
51
OUTPUT:
RESULT:
Thus the C++ program to implement multiple inheritances –access specifiers was
verified and executed successfully
52
EX NO: 11A UNARY OPERATOR AS MEMBER FUNCTION.
DATE: - 27/10/2022
AIM
To write a program to implement operator overloading using unary operator
ALGORITHM
53
PROGRAM:
class complex
{ int a, b, c;
public:
complex() {
}
void getvalue() {
cout << "Enter the Two Numbers:";
cin >> a>>b;
}
void operator++() {
a = ++a;
b = ++b;
}
void operator--() {
a = --a;
b = --b;
}
void display() {
54
SAMPLE OUTPUT:
RESULT:
Thus the C++ program to implement Unary operator as member function was verified and
executed successfully
55
EX 11B BINARY OPERATOR AS NON MEMBER FUNCTION.
DATE: - 29/10/2022
AIM:
TO implement binary operator without member function in C++
ALGORITHM:
56
PROGRAM:
//printing value
void printValue()
{
cout<<"Value is : "<<value<<endl;
}
};
//operator overloading function definition
Sample operator-(Sample &S1, Sample &S2)
{
Sample S;
S = S1.value-S2.value;
return S;
}
//main program
int main()
{
int i = 0;
//object declaration by calling parameterized constructor
Sample S1(600);
Sample S2(200);
Sample S3;
//subtracting objects (Binary - operator overloading)
S3 = S1 - S2;
cout<<"S1 :"<<endl;
57
S1.printValue();
cout<<"S2 :"<<endl;
S2.printValue();
cout<<"S3 :"<<endl;
S3.printValue();
getch();
return 0;
}
58
Sample Output:
RESULT:
Thus the C++ program to implement Binary operator as member function was verified and
executed successfully
59
EX 12a MEAN VALUE USING "FRIEND" FUNCTION.
DATE: - 29/10/2022
AIM:
To write a C++ program to find mean value of given numbers using "friend" Function.
ALGORITHM:
1) Start the program.
2) Declare a class "myclass" with a "friend" function.
3) Define the friend function.
4) Call the friend function from the main function using object as argument.
5) Display the Output.
6) Stop the program.
60
PROGRAM:
//Mean value using "friend" function
#include <iostream.h>
#include <conio.h>
class myclass
{
int val1,val2;
public:
void getdata()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend void mean(myclass m2);
};
void mean(myclass m2)
{
cout<<"Mean value: "<<(m2.val1 + m2.val2)/2;
}
void main()
{
clrscr();
myclass m1;
m1.getdata();
mean(m1);
getch();
}
61
OUTPUT:
RESULT:
Thus the C++ program to mean value of given numbers using "friend" Function has been
executed and verified successfully.
62
EX 12b AREA OF RECTANGLE AND SQUARE USING FRIEND CLASS.
DATE: - 31/10/2022
AIM
To write a program in C++ to implement friend class
ALGORITHM
63
PROGRAM
// To implement friend class
#include<iostream.h>
#include<conio.h>
class Rectangle
{
int L,B;
public:
Rectangle()
{
L=10;
B=20;
}
friend class Square; //Statement 1
};
class Square
{
int S;
public:
Square()
{
S=5;
}
void Display(Rectangle Rect)
{
cout<<"\n\n\tLength : "<<Rect.L;
cout<<"\n\n\tBreadth : "<<Rect.B;
cout<<"\n\n\tSide : "<<S;
}
};
void main()
{
Rectangle R;
Square S;
clrscr();
S.Display(R); //Statement
2 getch();
}
64
SAMPLE OUTPUT:
RESULT:
Thus the C++ program to find area of rectangle and square using friend class was verified
and executed successfully.
65
EX 13 CLASS TEMPLATE.
DATE: - 31/10/2022
AIM
To write a program in C++ to illustrate class template
ALGORITHM
66
PROGRAM :
// Class template with multiple parameters
#include<iostream.h>
#include<conio.h>
// Main Function
int main()
{
// instantiation with float and int type
Test <float, int> test1 (1.23, 123);
// instantiation with float and char type
Test <int, char> test2 (100, 'W');
clrscr();
test1.show();
test2.show();
getch();
return 0;
}
67
SAMPLE OUTPUT:
RESULT:
Thus the C++ program to implement class template was verified and executed
successfully.
68