PROGRAM # 1 COMPLEX NUMBER
CODING:
#include<iostream>
using namespace std;
class Complex
{
public:
int real, imag, scalar;
void setvalue()
{
cin>>real;
cin>>imag;
}
void display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
void sum(Complex c1, Complex c2)
{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
void sub(Complex c1, Complex c2)
{
real=c1.real-c2.real;
imag=c1.imag-c2.imag;
}
void mul(Complex c1, Complex c2)
{
real=c1.real*c2.real;
imag=c1.imag*c2.imag;
}
void mulscalar(Complex c1)
{
cout<<"Enter a scalar value"; cin>>scalar;
real=c1.real *scalar;
imag=c1.imag *scalar;
}
};
int main()
{
Complex c1,c2,c3;
cout<<"Enter real & imaginary part of first complex number"<<endl;
c1.setvalue();
cout<<"Enter real & imaginary part of second complex
number"<<endl;
c2.setvalue();
cout<<"Sum of two complex numbers is"<<endl; c3.sum(c1,c2);
c3.display();
cout<<"Difference of two complex numbers is"<<endl; c3.sub(c1,c2);
c3.display();
cout<<"Product of two complex numbers is"<<endl;
c3.mul(c1,c2);
c3.display();
c3.mulscalar(c1);
cout<<"Product of a complex number with the scalar value is"<<endl;
c3.display();
return 0;
}
OUTPUT:
PROGRAM #2 2-D POINT CLASS
CODING:
#include <iostream>
#include <cmath>
using namespace std;
class point {
public:
double x , y;
double setvalue()
{
cout<<"Enter two points:";
cin>>x>>y;
}
void showvalue()
{
cout<<"The points are: "<<x<<" and "<<y<<endl;
}
void check(point& other)
{
if(x==other.x && y==other.y) cout<<"The given points are equal";
else
cout<<"The given points are not equal";
}
double distanceTo(point& other)
{
return sqrt((x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));
}
};
int main()
{
point p1,p2;
p1.setvalue();
p2.setvalue();
p1.showvalue();
p2.showvalue();
p2.check(p1);
cout << "\nThe distance betwen 2d points p1 and p2 is " << p1.distanceTo(p2)
<< endl;
}
OUTPUT:
PROGRAM #3 HARMONIC PROGRESSION (HP)
CODING:
#include <iostream>
#include <vector>
using namespace std;
class ArithmeticProgression; // Forward declaration
class HarmonicProgression {
private:
int n; // Number of terms
vector<double> terms;
public:
HarmonicProgression(int n) : n(n) {}
// Function to generate HP up to n terms
void generateHP() {
terms.clear();
for (int i = 1; i <= n; ++i) {
terms.push_back(1.0 / i);
}
}
// Function to calculate the sum of HP up to n terms
double sumToN() {
double sum = 0;
for (int i = 0; i < n; ++i) {
sum += terms[i];
}
return sum;
}
// Function to calculate the sum of HP to infinity
double sumToInfinity() {
double sum = 0;
for (int i = 1; i <= n; ++i) {
sum += 1.0 / i;
}
return sum;
}
// Function to generate the nth term of the HP
double nthTerm(int term) {
return terms[term - 1];
}
// Friend function to generate corresponding AP
friend class ArithmeticProgression;
};
class ArithmeticProgression {
private:
int n; // Number of terms
vector<double> terms;
public:
ArithmeticProgression(const HarmonicProgression& hp) {
n = hp.n;
double sum = 0;
for (int i = 0; i < n; ++i) {
sum += hp.terms[i];
terms.push_back(sum);
}
}
// Function to display AP terms
void displayAP() {
cout << "Corresponding Arithmetic Progression:" << endl;
for (int i = 0; i < n; ++i) {
cout << terms[i] << " ";
}
cout << endl;
}
};
int main() {
int n;
cout << "Enter the number of terms for Harmonic Progression: ";
cin >> n;
HarmonicProgression hp(n);
hp.generateHP();
cout << "Harmonic Progression up to " << n << " terms:" << endl;
for (int i = 1; i <= n; ++i) {
cout << hp.nthTerm(i) << " ";
}
cout << endl;
cout << "Sum of HP up to " << n << " terms: " << hp.sumToN() << endl;
cout << "Sum of HP to infinity: " << hp.sumToInfinity() << endl;
// Generating corresponding AP
ArithmeticProgression ap(hp);
ap.displayAP();
return 0;
}
OUTPUT:
PROGRAM #4 CALCULATE VOLUME & SURFACE AREA
FOR DIFFERENT SOLIDS
CODING:
#include <iostream>
using namespace std;
const float pi=3.14;
class solid
{
public:
int shape;
float vol, area, radius, height, side;
void chooseshape()
{
cout<<"Choose the Solid to which you want to find volume & Area: \n 1. Sphere\n 2. Cube
\n3. Cylinder\n";
cin>>shape;
}
void volume()
{
switch(shape)
{
case 1:
cout << "Enter the radius of Sphere : ";
cin >> radius;
vol = pi * radius * radius * radius * 4 / 3;
cout<<"Volume of Sphere is "<<vol;
break;
case 2:
cout << "Enter Cube's side :";
cin >> side;
vol = side * side * side;
cout<<"The Volume of Cube is "<<vol;
break;
case 3:
cout << "Enter Cylinder's radius : ";
cin >> radius;
cout << "Enter Cylinder's height : ";
cin >> height;
vol = pi * radius * radius * height;
cout<<"The Volume of Cylinder is "<<vol;
break;
default: cout<<"\nInvalid Choice";
}
}
void surfacearea()
{
switch(shape)
{
case 1:
area = 4 * pi * radius * radius;
cout<<"\nThe Surface Area of Sphere is "<<area;
break;
case 2:
area = 6 * side * side;
cout<<"\nThe Surface Area of Cube is "<<area;
break;
case 3:
area = (2*pi *radius*radius) + (2*pi*radius*height);
cout<<"\nThe Surface Area of Cylinder is "<<area;
break;
default: cout<<"\nInvalid Choice";
}
}
};
int main()
{
solid s;
s.chooseshape();
s.volume();
s.surfacearea();
}
OUTPUT:
PROGRAM #5 TIME MANIPULATION
CODING:
OUTPUT:
PROGRAM #6 MATRIX MANIPULATION
CODING:
#include<iostream>
using namespace std;
class Matrix
{
static int count;
int a[3][3];
public:
Matrix()
{
count++;
}
static int totalObjects(void)
{
return count;
}
void accept();
void display();
void operator +(Matrix x);
void operator *(Matrix x);
};
int Matrix::count = 0;
void Matrix::accept()
{
cout<<"\n Enter Matrix Element (3 X 3) : \n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<" ";
cin>>a[i][j];
}}}
void Matrix::display()
{
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}}
void Matrix::operator +(Matrix x)
{
int mat[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat[i][j]=a[i][j]+x.a[i][j];
}}
cout<<"\n Addition of Matrix : \n\n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<"\n";
}
}
void Matrix::operator *(Matrix x)
{
int mat[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat[i][j]=a[i][j]*x.a[i][j];
}
}
cout<<"\n Multiplication of Matrix : \n\n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<"\n";
}}
int main()
{
Matrix m,n;
m.accept(); // Accepting Rows
n.accept(); // Accepting Columns
cout<<"\n First Matrix : \n\n";
m.display(); // Displaying First Matrix
cout<<"\n Second Matrix : \n\n";
n.display(); // Displaying Second Matrix
m+n; // Addition of Two Matrices. Overloaded '+' Operator
m*n;
cout << "Total objects created: " << Matrix::totalObjects() << endl;
return 0;
}
OUTPUT:
PROGRAM #7 STRING MANIPULATION
CODING:
#include <iostream>
#include <cstring>
using namespace std;
class cString
{
private:
static const int MAX_LENGTH = 100;
char str[MAX_LENGTH + 1]; // +1 for null terminator public:
// Default Constructor
cString()
{
str[0] = '\0'; // Initialize as an empty string
}
// Parameterized Constructor cString(const char* input_str) {
strncpy(str, input_str, MAX_LENGTH);
str[MAX_LENGTH] = '\0'; // Ensure null terminator
}
// Copy Constructor
cString(const cString& other)
{
strncpy(str, other.str, MAX_LENGTH);
str[MAX_LENGTH] = '\0'; // Ensure null terminator
}
// Function to concatenate two strings
cString concatenate(const cString& other) const { cString result(*this); // Copy
constructor used
strncat(result.str, other.str, MAX_LENGTH - strlen(result.str));
return result;
}
// Function to find the length of the string
int length() const {
return strlen(str);
}
// Function to reverse the string
cString reverse() const {
cString result(*this); // Copy constructor used
int len = strlen(result.str);
for (int i = 0; i < len / 2; ++i)
{
char temp = result.str[i];
result.str[i] = result.str[len - i - 1];
result.str[len - i - 1] = temp;
}
return result;
}
// Function to compare two strings
int compare(const cString& other) const {
return strcmp(str, other.str);
}
// Display the string void display() const {
cout << str;
}
};
int main() {
cString s1("Hello"), s2("World");
cout << "\ns1: ";
s1.display();
std::cout << "\ns2: ";
s2.display();
cString concatenated = s1.concatenate(s2);
cout << "\nConcatenated string: ";
concatenated.display();
std::cout << "\nLength of concatenated string: " << concatenated.length();
cString reversed = s1.reverse();
std::cout << "\nReversed s1: ";
reversed.display();
cout<<"\nComparison of s1 and s2: ";
if(s1.compare(s2)==0)
cout<<"Equal Strings!";
else
cout<<"Not Equal Strings!";
return 0;
}
OUTPUT:
PROGRAM #8 STRING MANIPULATION WITH
DYNAMIC MEMORY ALLOCATION
CODING:
#include <iostream>
#include <cstring>
using namespace std;
class cString {
private:
char* str;
public:
// Constructor
cString(const char* s = "") {
int len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
}
// Copy Constructor
cString(const cString& other) {
int len = strlen(other.str);
str = new char[len + 1];
strcpy(str, other.str);
}
// Destructor
~cString() {
delete[] str;
}
// Concatenate two strings
cString concatenate(const cString& other) const {
int len1 = strlen(str);
int len2 = strlen(other.str);
char* newStr = new char[len1 + len2 + 1];
strcpy(newStr, str);
strcat(newStr, other.str);
cString result(newStr);
delete[] newStr;
return result;
}
// Find the length of the string
int length() const {
return strlen(str);
}
// Reversing a string
cString reverse() const {
int len = strlen(str);
char* reversedStr = new char[len + 1];
for (int i = 0; i < len; ++i) {
reversedStr[i] = str[len - i - 1];
}
reversedStr[len] = '\0';
cString result(reversedStr);
delete[] reversedStr;
return result;
}
// Comparing two strings
int compare(const cString& other) const {
return strcmp(str, other.str);
}
// Display the string
void display() const {
cout << str << endl;
}
};
int main() {
cString s1("Hello");
cString s2("World");
cout << "s1: ";
s1.display();
cout << "s2: ";
s2.display();
cout << "Concatenation of s1 and s2: ";
cString s3 = s1.concatenate(s2);
s3.display();
cout << "Length of s1: " << s1.length() << endl;
cout << "Reversed s1: ";
cString s4 = s1.reverse();
s4.display();
cout << "Comparison of s1 and s2: " << s1.compare(s2) << endl;
return 0;
}
OUTPUT:
PROGRAM #9 AREA OF FIGURES USING
RUN-TIME POLYMORPHISM
CODING:
#include<iostream>
using namespace std;
class Shape
{
public: double a,b;
void get_data ()
{
cin>>a>>b;
}
virtual void display_area () = 0;
};
class Triangle:public Shape
{
public: void display_area ()
{
cout<<"Area of triangle "<<0.5*a*b<<endl;
}
};
class Rectangle:public Shape
{
public: void display_area ()
{
cout<<"Area of rectangle "<<a*b<<endl;
}
};
class Circle:public Shape
{
public: void display_area ()
{
cout<<"Area of circle "<<3.14*a*a<<endl;
}
};
int main()
{
Triangle t; Shape *st = &t;
cout<<"Enter base and altitude: ";
st->get_data();
st->display_area();
Rectangle r;
Shape *sr = &r;
cout<<"Enter length and breadth: ";
sr->get_data();
sr->display_area();
Circle c;
Shape *sc = &c;
cout<<"Enter Pi value and radius: ";
sc->get_data();
sc->display_area(); return 0;
}
OUTPUT:
PROGRAM # 10 SORTING AN ARRAY
CODING:
#include <iostream>
#include <stdexcept>
using namespace std;
template<typename T>
class Array {
private:
T* arr;
int size;
public:
// Constructor
Array(int sz) : size(sz) {
arr = new T[size];
}
// Destructor
~Array() {
delete[] arr;
}
// Function to access array elements with bounds checking
T& operator[](int index) {
if (index < 0 || index >= size) {
throw out_of_range("Index out of range");
}
return arr[index];
}
// Function to sort array elements
void sort() {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
};
int main() {
try {
Array<int> intArray(5);
cout << "Enter 5 integer elements: ";
for (int i = 0; i < 5; ++i) {
cin >> intArray[i];
}
cout << "Array before sorting: ";
for (int i = 0; i < 5; ++i) {
cout << intArray[i] << " ";
}
cout << endl;
intArray.sort();
cout << "Array after sorting: ";
for (int i = 0; i < 5; ++i) {
cout << intArray[i] << " ";
}
cout << endl;
// Accessing out-of-bound element
// Uncomment below line to test
// cout << intArray[10] << endl;
} catch (const out_of_range& e)
{
cout << "Exception: " << e.what() << endl;
}
return 0;
}
OUTPUT:
PROGRAM #11 VECTOR STL CONTAINER
CODING:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Declaration and initialization of a vector of integers
vector<int> numbers = {1, 2, 3, 4, 5};
// Add elements to the vector using push_back
numbers.push_back(6);
numbers.push_back(7);
// Displaying the elements of the vector
cout << "Vector elements: ";
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
// Accessing elements by index
cout << "Element at index 3: " << numbers[3] << endl;
// Size of the vector
cout << "Size of vector: " << numbers.size() << endl;
// Iterating over the vector using iterators
cout << "Vector elements using iterators: ";
for (auto it = numbers.begin(); it != numbers.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// Clearing the vector
numbers.clear();
cout << "Vector cleared. Size: " << numbers.size() << endl;
// Checking if the vector is empty
cout << "Is vector empty? " << (numbers.empty() ? "Yes" : "No") <<
endl;
return 0;
}
OUTPUT:
PROGRAM # 12 TELEPHONE DIRECTORY USING FILES
CODING:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Contact {
string name;
string phoneNumber;
};
// Function prototypes
void addContact(ofstream& file);
void searchContact(ifstream& file, const string& name);
void displayAllContacts(ifstream& file);
int main() {
ofstream outFile("contacts.txt", ios::app); // File to store contacts
if (!outFile) {
cerr << "Error opening file!" << endl;
return 1;
}
int choice;
do {
cout << "Telephone Directory" << endl;
cout << "1. Add Contact" << endl;
cout << "2. Search Contact" << endl;
cout << "3. Display All Contacts" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addContact(outFile);
break;
case 2:
{
ifstream inFile("contacts.txt");
if (!inFile) {
cerr << "Error opening file!" << endl;
return 1;
}
string name;
cout << "Enter name to search: ";
cin >> name;
searchContact(inFile, name);
inFile.close();
}
break;
case 3:
{
ifstream inFile("contacts.txt");
if (!inFile) {
cerr << "Error opening file!" << endl;
return 1;
}
displayAllContacts(inFile);
inFile.close();
}
break;
case 4:
cout << "Exiting program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4);
outFile.close();
return 0;
}
// Function to add a contact to the file
void addContact(ofstream& file) {
Contact contact;
cout << "Enter name: ";
cin >> contact.name;
cout << "Enter phone number: ";
cin >> contact.phoneNumber;
file << contact.name << " " << contact.phoneNumber << endl;
cout << "Contact added successfully." << endl;
}
// Function to search for a contact in the file
void searchContact(ifstream& file, const string& name) {
Contact contact;
bool found = false;
while (file >> contact.name >> contact.phoneNumber) {
if (contact.name == name) {
found = true;
cout << "Name: " << contact.name << ", Phone Number: " <<
contact.phoneNumber << endl;
break;
}
}
if (!found)
cout << "Contact not found." << endl;
}
// Function to display all contacts in the file
void displayAllContacts(ifstream& file) {
Contact contact;
cout << "All Contacts:" << endl;
while (file >> contact.name >> contact.phoneNumber) {
cout << "Name: " << contact.name << ", Phone Number: " <<
contact.phoneNumber << endl;
}
}
OUTPUT: