Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views37 pages

OOP Practical Assignments by Bhavesh Chavda

The document outlines practical assignments for a B.Tech CE student named Zala Rajvir, detailing various programming tasks in Object Oriented Programming under the guidance of Mr. Bhavesh Chavda. Each assignment includes a specific programming challenge, such as calculating sums, swapping numbers, and working with classes and objects in C++. The document provides both the assignment descriptions and sample code outputs for each task.

Uploaded by

lierhunter2468
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)
36 views37 pages

OOP Practical Assignments by Bhavesh Chavda

The document outlines practical assignments for a B.Tech CE student named Zala Rajvir, detailing various programming tasks in Object Oriented Programming under the guidance of Mr. Bhavesh Chavda. Each assignment includes a specific programming challenge, such as calculating sums, swapping numbers, and working with classes and objects in C++. The document provides both the assignment descriptions and sample code outputs for each task.

Uploaded by

lierhunter2468
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

NAME: Zala Rajvir

Enroll/GR No: 15616

PROGRAM NAME:- [Link] CE SEM-III

COURSE CODE: - CE302P

COURSE:- OBJECT ORIENTED PROGRAMMING


PRACTICAL

FACULTY NAME:- MR. BHAVESH CHAVDA


Sr.N
Date Practical Name Sign
o
19.07.202 Write A Program To Take Two Numbers As Input And Print Their
1
4 Sum And Average.
02.08.202 Write A Program To Swap Two Numbers Without Using A Third
2
4 Variable.
09.08.202 Write A Program To Check Whether The Number Provided Is Even
3
4 Or Odd.
16.08.202 Write A Program To Print The Largest Number Among Three
4
4 Numbers Given By The User
23.08.202 Write A Single Program That Provides The Sum, Difference,
5
4 Multiplication And Division Of Two Numbers.
06.09.202
6 Write A Program To Print Table Of Any Number Using A For Loop.
4
06.09.202
7 Write A Program To Print Table Of A Number Using Do While Loop.
4
13.09.202 Write A Program To Print Fibonacci Series (0, 1, 1, 2, 3, 5, 8, 13,
8
4 21,...).
13.09.202
9 Write A Program To Reverse A Number.
4
20.09.202 Write A Program To Check Whether A Number Is A Prime Number
10
4 Or Not.
20.09.202
11 Write A Program To Convert Binary Number To Decimal Number
4
20.09.202 Write A Program That Takes Values In An Array And Also Display
12
4 Them.
27.09.202
13 Write A Program To Print Length Of A String Provided
4
27.09.202
14 Write A Program To Check Whether A String Is Palindrome Or Not
4
27.09.202
15 Write A Program That Provides The Sum Of Two Matrices
4
04.10.202
16 Write A Program To Find Out The Product Of Two Matrices
4
04.10.202 Write A Program To Print ASCII Value Of Digits, Uppercase And
17
4 Lowercase Alphabets
04.10.202
18 Write A Program To Demonstrate The Use Of Class And Object
4
11.10.202
19 Write A Program To Demonstrate The Use Of Constructor In a Class
4
11.10.202 Write A Program To Demonstrate The Use Of Constructor And
20
4 Destructor In A Class
11.10.202
21 Write A Program To Demonstrate The Use Of Static Variable
4
18.10.202
22 Write A Program To Swap Two Numbers Using Class
4
23 18.10.202 Write A Program To Print Numbers From 1 To N Using Class
4
18.10.202
24 Write A Program To Overload A Sum Function
4
25.10.202 Write A Program To Calculate Area Of A Circle, A Rectangle Or A
25
4 Triangle Depending On Input Using Overloaded Calculate Function
25.10.202
26 Write A Program To Print Factorial Of A Given Number Using Class
4
25.10.202
27 Write A Program To Check Whether A Year Is Leap Year Or Not.
4
08.11.202
28 Write A C++ Program To Create A File ([Link])
4
08.11.202
29 Write A Program For Creating And Writing On A Text File
4
08.11.202
30 Write A Program To Retrieve/Read Data From A Text File
4
Assignment - C++
Program 1: Write a program to take two numbers as
input and print their sum and average.

Output:

Program 2: Write a program to swap two numbers


without using a third Variable.
#include
<iostream> using
namespace std;

int main() {
int a, b;
cout << "Enter two numbers separated by space:
"; cin >> a >> b;

// Swap without a third


variable a = a + b;
b = a - b;
a = a - b;

cout << "After swapping: a = " << a << ", b = " << b <<
endl; return 0;
}

Output:
Enter two numbers separated by space: 5
10 After swapping: a = 10, b = 5

Program 3: Write a program to check whether the


number provided is even or odd.

#include
<iostream> using
namespace std;

int main()
{
int num;
cout << "Enter a number: ";
Output:

Program 4: Write a program to print the largest


number among three numbers given by the user.

Output:
Program 5: Write a single program that provides the
sum, difference, multiplication and division of two
numbers.

Output:
Program 6: Write a program to print table of any
number using a for loop.

Output:

Enter a num
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15

Program 7: Write a program to print table of a number


using do while loop
int num, i = 1;
cout << "Enter a number to print its table:
"; cin >> num;

do { cout << num << " x " << i


<< " = " << num * i <<
endl; i++;
} while (i <= 10);

return 0;
}

Output:

Enter a number to print its table: 7


7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
...
7 x 10 = 70

Program 8: Write a program to print Fibonacci Series

#include
<iostream> using
namespace std;

int main() {
int n, a = 0, b = 1, next;
cout << "Enter the number of terms:
"; cin >> n;
Output:

Program 9: Write a program to reverse a number.

Output:
Program 10: Write a program to check whether a
number is a prime number or not.
Output:

Program 11: Write a program to convert binary


number to decimal number.

Output:
Program 12: Write a program that takes values in an
array and also display them.

Output:

Enter the number of elements: 5


Enter 5 elements:
10 20 30 40 50
The elements in the array are: 10 20 30 40 50

Program 13: Write a program to print length of a


string provided.

#include
<iostream>
#include
<string>
using namespace std;

int main() {
string str;

cout << "Enter a string: ";


getline(cin, str); // Take input with spaces

cout << "The length of the string is: " << [Link]() << endl;

return 0;
}

Output:

Enter a string: Hello,


World! The length of the
string is: 13

Program 14:Write a program to check whether a string


is palindrome or not.

#include
<iostream>
#include
<string>
using namespace std;

int main() {
string str, reversedStr;

cout << "Enter a string: ";


cin >> str; // Take input (single word without spaces)
reversedStr = string([Link](), [Link]()); // Reverse the
string if (str == reversedStr) {
cout << "The string is a palindrome!" << endl;
} else {
cout << "The string is not a palindrome." << endl;
}

return 0;
}

Output:
Enter a string: madam
The string is a palindrome!

Program 15: Write a program that provides the sum of


two matrices.

#include
<iostream> using
namespace std;
cin >> rows;
cout << "Enter the number of columns:
"; cin >> cols;

int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];

// Input for Matrix 1


cout << "Enter elements of Matrix 1:" <<
endl; for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols;
j++) { cin >>
matrix1[i][j];
}
}

// Input for Matrix 2


cout << "Enter elements of Matrix 2:" <<
endl; for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols;
j++) { cin >>
matrix2[i][j];
}
}

// Calculating Sum
Output:

Program 16: Write a program to find out the product


of two matrices.
int matrix1[rows1][cols1],
matrix2[rows2][cols2], product[rows1][cols2] =
{0};

// Input for Matrix 1


cout << "Enter elements of Matrix 1:" <<
endl; for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1;
j++) { cin >>
matrix1[i][j];
}
}

// Input for Matrix 2


cout << "Enter elements of Matrix 2:" <<
endl; for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2;
j++) { cin >>
matrix2[i][j];
}
}

// Multiplying matrices
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
Output:

Program 17: Write a program to print ASCII value of


digits, uppercase and lowercase alphabets.
}

return 0;
}

Output:
ASCII values of digits:
0: 48
1: 49
2: 50
3: 51
4: 52
5: 53
6: 54
7: 55
8: 56
9: 57

ASCII values of uppercase


alphabets: A: 65
B: 66
C: 67
..
.
Z:
90

Program 18: Write a program to demonstrate the use


of class and object.
#include
<iostream> using
namespace std;

// Define a
class class
Student {
public:
string name; // Data member to store
name int age; // Data member to
store age

// Member function to input student


details void inputDetails() {
cout << "Enter student's name:
"; cin >> name;
cout << "Enter student's age:
"; cin >> age;
}

// Member function to display student


details void displayDetails() {
cout << "Student's Name: " << name <<
endl; cout << "Student's Age: " << age
<< endl;
}
};

Output:
Enter student's name:
John Enter student's
age: 20 Displaying
Student Details:
Student's Name:
John Student's
Age: 20

Program 19: Write a program to demonstrate the use


of constructor in a class.

#include
<iostream> using
namespace std;

// Define a
class class
Student {
private:
string
name; int
age;

public:
// Constructor to initialize data members
Student(string studentName, int studentAge) {
name =
studentName; age
= studentAge;
}

// Member function to display student


details void displayDetails() {
// Display details using the object
cout << "Displaying Student Details:" <<
endl; [Link]();

return 0;
}

Output:

Displaying Student Details:


Student's Name:
John Student's
Age: 20

Program 20: Write a program to demonstrate the use


of constructor and destructor in a class.

#include
<iostream> using
namespace std;

// Define a
class class
Student {
private:
string
name; int
age;

public:
// Constructor to initialize data members
Student(string studentName, int studentAge) {
name =
studentName; age
= studentAge;
cout << "Constructor called: Object created and initialized." <<
cout << "Student's Age: " << age << endl;
}

// Destructor to clean up when the object is destroyed


~Student() {
cout << "Destructor called: Object destroyed." << endl;
}
};

int main() {
// Create an object of Student
class Student s1("Alice", 22);

// Display details using the object


cout << "Displaying Student Details:" <<
endl; [Link]();

return 0; // Destructor is automatically called here


}

Output:
Constructor called: Object created and
initialized. Displaying Student Details:
Student's Name:
Alice Student's
Age: 22
Destructor called: Object destroyed.

Program 21: Write a program to demonstrate the use


of static variable.
public:
// Constructor to increment count when an object is
created Counter() {
count++;
}

// Member function to display the


count void displayCount() {
cout << "Number of objects created: " << count << endl;
}
};

// Initialize the static


variable int Counter::count =
0;

int main() {
Counter c1; // First
object
[Link]();

Counter c2; // Second


object [Link]();

Counter c3; // Third object

Output:

Program 22: Write a program to swap two numbers


using class
#include
<iostream> using
namespace std;

class
Swapper {
private:
int a, b;

public:
// Constructor to initialize
numbers Swapper(int num1, int
num2) {
a =
num1;
b =
num2;
}

// Member function to swap the


numbers void swapNumbers() {
int temp =
a; a = b;
b = temp;
}

// Member function to display the


numbers void displayNumbers() {
cout << "a: " << a << ", b: " << b << endl;
[Link]();

cout << "After swapping:" <<


endl; [Link]();

return 0;
}

Output:
Enter two numbers:
5 10
Before swapping:
a: 5, b: 10
After swapping:
a: 10, b: 5

Program 23: Write a Program to Print Numbers from 1


to n using class

#include
<iostream> using
namespace std;

class
NumberPrinter {
private:
int n; // Member to store the upper limit

public:
// Constructor to
initialize n
NumberPrinter(int limit) {
n = limit;
cout << i << " ";
}
cout << endl;
}
};

int main() {
int limit;

cout << "Enter the value of n:


"; cin >> limit;

// Create an object of NumberPrinter


class NumberPrinter printer(limit);

// Call the function to print


numbers [Link]();

return 0;
}

Output:

Enter the value of n:


10 Numbers from 1 to
10 are:
1 2 3 4 5 6 7 8 9 10

Program 24: Write a program to overload a sum


function.

#include <iostream>
}

// Function to calculate the sum of two floating-point


numbers float sum(float a, float b) {
return a + b;
}

// Function to calculate the sum of three


integers int sum(int a, int b, int c) {
return a + b + c;
}

int main() {
// Calling the sum function for two integers
cout << "Sum of 10 and 20 (integers): " << sum(10, 20) << endl;

// Calling the sum function for two floats


cout << "Sum of 5.5 and 2.5 (floats): " << sum(5.5f, 2.5f) << endl;

// Calling the sum function for three integers


cout << "Sum of 1, 2, and 3 (integers): " << sum(1, 2, 3) << endl;

return 0;
}

Output:

Program 25 : Write a program to calculate area of a


circle, a rectangle or a triangle depending on input
using overloaded calculate function.
// Function to calculate the area of a
circle float calculate(float radius) {
return 3.14159 * radius * radius;
}

// Function to calculate the area of a


rectangle float calculate(float length, float
breadth) {
return length * breadth;
}

// Function to calculate the area of a triangle


float calculate(float base, float height, int)
{
return 0.5 * base * height;
}

int main() {
int choice;
cout << "Choose the shape to calculate area:" <<
endl; cout << "1. Circle" << endl;
cout << "2. Rectangle" <<
endl; cout << "3. Triangle"
<< endl; cin >> choice;
case 3: {
float base, height;
cout << "Enter the base
and height of the
triangle: "; cin >> base
<< endl; >> height;
cout << "Area of the triangle: "
<< calculate(base, height, 0)

break;
}
default:
cout << "Invalid choice!" << endl;
}

return 0;
}

Output:
Choose the shape to calculate area:
1. Circle
2. Rectangle
3. Tr
iangle 1
Enter the radius of the
circle: 5 Area of the circle:
78.5398

Program 26 : Write a program to print factorial of a


given number using class.
#include
<iostream> using
namespace std;
number = num;
}

// Member function to calculate the


factorial long long calculateFactorial() {
long long factorial = 1;
for (int i = 1; i <= number;
i++) { factorial *= i;
}
return factorial;
}

// Member function to display the


factorial void displayFactorial() {
cout << "The factorial of " << number << " is: "
<< calculateFactorial() << endl;
}
};

int main()
{
int num;

cout << "Enter a number to calculate its factorial:


"; cin >> num;

Output:
Program 27 : Write a program to check whether a year
is leap year or not.

Output:

Program 28 : Write a C++ program to create a file


([Link]).
using namespace std;

int main() {
// Create an ofstream object to write to a
file ofstream outFile("[Link]");

// Check if the file is successfully


created if (outFile.is_open()) {
cout << "File '[Link]' created successfully!" << endl;

// Write data to the file


outFile << "Hello, this is a test file.\n";
outFile << "File handling in C++ is easy and powerful.\n";

// Close the
file
[Link](
);

cout << "Data written to '[Link]' successfully!" << endl;


}
else
{
cout << "Failed to create the file!" << endl;
}

return 0;
}

Output:

Program 29 : Write a program for creating and writing


on a text file.
string data; // Variable to store user input

// Create and open a file for


writing ofstream
outFile(fileName);

// Check if the file is successfully


created if (outFile.is_open()) {
cout << "File '" << fileName << "' created successfully!" <<
endl;

// Ask user for data to write


cout << "Enter data to write into the file:
"; getline(cin, data);

// Write the input data into the


file outFile << data << endl;

/
/
C

endl; l
o
s
e
t
h
e
f
i
l
e
o
u
t
F
i
l
e
.
c
l
o
s
e
(
)
; cout << "Data written to '" <<
fileName << "' successfully!" <<
} else {
cout << "Failed to create the file!" << endl;
}

Output:

Program 30 : Write a program to retrieve/read data from


a text file
#include <iostream>
#include <fstream> // For file
handling using namespace std;

int main() {
string fileName = "[Link]"; // File to read
from string line; // Variable to store each line
read

// Open the file for


reading ifstream
inFile(fileName);

// Check if the file exists and is


open if (inFile.is_open()) {
cout << "Reading data from '" << fileName << "':" << endl;

// Read and display the file line by line


while (getline(inFile, line)) {
cout << line << endl;
}

// Close the
file
[Link]()
;
} else {
cout << "Failed to open the file '" << fileName << "'!" << endl;

Output:

You might also like