KEMBAR78
Oops Lab | PDF | Namespace | Computer Science
0% found this document useful (0 votes)
35 views66 pages

Oops Lab

CACASDF
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)
35 views66 pages

Oops Lab

CACASDF
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
You are on page 1/ 66

CS2113: Object Oriented Programming Lab

REPORT

Instructor : Dr. Moirangthem Dennis Singh


Dr. Rajkumari Bidyalakshmi Devi

Submitted by- Shailendra Shukla || Roll No.-220102009

Submitted on 21st of December 2023

In the Department of Computer Science


and
Engineering
Contents Table
Lab No. Page No.
Lab 1 3–6

Lab 2 7 - 13

Lab 3 14 - 21

Lab 4 22 - 24

Lab 5 25 – 27

Lab 6 28 – 36

Lab 7 37 – 45

Lab 8 46 - 54

Lab 9 55 - 63

Lab 10 64 - 66

Lab 11

2|Page
LAB-1
1. Write a C++ program to add two numbers.
#include <iostream> using
namespace std; int main() {

int num1, num2, sum;

cout << "Enter two integers: "; cin >>


num1 >> num2;
sum = num1 + num2;

cout << num1 << " + " << num2 << " = " << sum; return 0;
}

OUTPUT

3|Page
Write A C++ program to multiply two numbers.
#include <iostream>
using namespace std;
int main() {

int num1, num2, mult;

cout << "Enter two integers: "; cin >>


num1 >> num2;
mult = num1 * num2;

cout << num1 << " * " << num2 << " = " << mult; return 0;

OUTPUT

4|Page
Write A C++ program to print number entered by a user.

#include <iostream>

int main() { int


n;
std::cout << "Enter a number: "; std::cin >> n;
std::cout << "Input value is: " << n << std::endl;
return 0;

OUTPUT

5|Page

Write a C++ program to find the quotient and remainder.


#include <iostream>

int main()
{

int a, b,quotient,remainder;

std::cout << "Enter dividend: "; std::cin


>>a;
std::cout << "Enter divisor: "; std::cin >>
b;
quotient = a / b; remainder
= a % b;
std::cout << "Quotient: " << quotient << std::endl; std::cout <<
"Remainder: " << remainder << std::endl; return 0;
}

OUTPUT

6|Page
LAB-2
Write a C++ program to find the largest of two nos
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout << "Enter two numbers to be compared" << endl;
cin >> a >> b;
if (a>b)
{
cout << "Largest number is: " << a << endl;
}
else if(b>a)
{
cout << "Largest number is: " << b << endl;
}
else
{
cout << "Both no.s are equal" << endl;
}
return 0;
}

7|Page
Write a C++ program to find the largest of three nos.

#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
cout << "Enter three numbers to be compared" << endl;
cin >> a >> b >>c;
if (a >= b && a >= c)
{
cout << "The largest number is: " << a << endl;
}
else if (b >= a && b >= c)
{
cout << "The largest number is: " << b << endl;
}
else
{
cout << "The largest number is: " << c << endl;
}

return 0;
}

8|Page
1. Write a C++ program to calculate the roots of a quadratic equation.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
double d, r1, r2;
cout << "The quadratic equation is ax^2+bx+c, where x is root
and a,b,c are coefficients\n";
cout << "\n";
cout << "Enter coefficient a: "<< endl; cin
>> a;
cout << "Enter coefficient b: "<< endl; cin
>> b;
cout << "Enter coefficient c: "<< endl; cin
>> c;
d= (b * b) - (4 * a * c);
if(d>0)
{
r1 = ((-b) + sqrt(d)) / (2 * a);
r2 = ((-b) - sqrt(d)) / (2 * a);
cout << "1st Root: " << r1 << endl;
cout << "2nd Root: " << r2 << endl;
}
else if (d == 0)
{
r1 = -b / (2 * a);

9|Page
cout << "Both roots are equal and their value is: " << r1 <<
endl;
}
else
{
cout << "Roots are complex " << endl;
}

return 0;
}

10 | P a g
e
Write a C++ program to determine whether an entered character is a
vowel or not.

#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;
if(ch=='A' || ch=='a'|| ch=='E' || ch=='e'|| ch=='I'|| ch=='i'|| ch=='O'|| ch=='o'|| ch=='U'||
ch=='u')
{
cout << ch << " is a vowel." << endl;
}
else
{
cout << ch << " is not a vowel." << endl;
}
return 0;
}

11 | P a g
e
Write a C++ program to enter a number from 1-7 and display the corresponding day
of the week using switch case statement.

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a number from 1 to 7: ";
cin >> n;
switch (n)
{
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid input. Please enter a number from 1 to 7." << endl;
}

return 0;
}

12 | P a g
e
13 | P a g
e
LAB-3
Write a C++ program to print the pattern of

1. Right Triangle

CODE:

#include <iostream> int


main()
{
int row;
std::cout << "Enter the number of rows: "; std::cin >>
row;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= i; j++)
{
std::cout << "*";
}
std::cout << std::endl;
}

return 0;
}

OUTPUT

14 | P a g
e
2. Inverted Right Triangle.

CODE:

#include <iostream> int

main() {
int n;
std::cout << "Enter the number of rows: "; std::cin >>
n;

for (int i = n; i >= 1; i--)


{
for (int j = 1; j <= i; j++)
{
std::cout << "*";
}
std::cout << std::endl;
}

15 | P a g
e
return 0;
}

OUTPUT

3. Equilateral Triangle

CODE:

#include <iostream> int


main()
{
int n;
std::cout << "Enter the number of rows: "; std::cin >>
n;

for (int i=1; i<=n; i++)


{
for (int j=1; j<=n-i; j++)
16 | P a g
e
{
std::cout << " ";
}
for (int k=1; k<=2*i-1; k++)
{
std::cout << "*";
}
std::cout << std::endl;
}
return 0;
}
OUTPUT

17 | P a g
e
4. Inverted Equilateral Triangle.

CODE:

#include <iostream> int


main()
{
int n;
std::cout << "Enter the number of rows: "; std::cin >> n;
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= n - i; j++)
{
std::cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++)
{
std::cout << "*";
}
std::cout << std::endl;
}
return 0;
}

OUTPUT

18 | P a g
e
19 | P a g
e
5. Inverted Mirrored Right Triangle.

CODE:

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter no. of rows: ";
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i-1;j++)
{
cout<<" ";
}
for(int k=1;k<=n-i+1;k++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}

20 | P a g
e
OUTPUT

21 | P a g
e
LAB-4
Write a C++ program to enter a decimal number. Calculate and display the binary
equivalent of this number.

CODE:
#include <iostream>
using namespace std;
int main()
{
int dec, bin = 0, r, p = 1;
cout << "Enter a number to be converted: ";
cin >> dec;
while (dec != 0)
{
r = dec % 2;
bin = bin + (r * p);
dec = dec / 2;
p *= 10;
}
cout << "The number in the binary form is: " << bin ;
return 0;
}

OUTPUT

22 | P a g
e
1. Write a C++ program to print the reverse of a number.
CODE:
#include <iostream>
using namespace std;
int main()
{
int num, rev = 0, d;
cout << "Enter a number to be reversed: ";
cin >> num;
while (num != 0)
{
d = num % 10;
rev = rev * 10 + d;
num /= 10;
}

cout << "Reverse of the number: " << rev << endl;

return 0;
}

OUTPUT:

23 | P a g
e
2. Write a C++ program to enter a number and then calculate the sum of its digit.

CODE:
#include <iostream>
using namespace std;
int main()
{
int n, temp, sum = 0, d;
cout << "Enter an integer: ";
cin >> n;
temp = n;

while (n != 0) {
d = n % 10;
sum += d;
n /= 10;
}

cout << "The sum of the digits of " << temp << " is: " << sum << endl;

return 0;
}

OUTPUT:

24 | P a g
e
LAB-5
Write a simple C++ program with a Class having multiple variables and functions to
demonstrate:

1. Private and Public access specifiers.


2. Declaration and definition of functions for the class
3. Constructors and destructors
4. Inline function definition inside a class
5. Creation of an object and using the object to access the variables/functions of the classes

#include<bits/stdc++.h>
using namespace std;

class animal
{
private:
int weight;
public:
// state or properties
int age;
string type;

// parameterised constructor

animal(int age , int weight , string type)


{
this -> age = age;
this -> weight = weight;
this -> type = type;
cout<<"constructor called"<<endl;
}

// class functions

void sleep(){
cout<<"sleeping"<<endl;
}
void eat(){
cout<<"Eating"<<endl;
}

25 | P a g
e int getweight(){
return weight ;
}
this->weight = weight;
}
inline void print()
{
cout<<age<<" "<<weight<<" "<<" "<<type<<endl;
}

// destructor

~animal(){
cout<<"i am inside destructor i.e object destroyed"<<endl ;
}

};

int main()
{

animal *a = new animal(10 , 100, "Lion");


a->print();

// destructor

cout<<"b object creation"<<endl;


animal*b = new animal(13 , 100 , "Deer");
delete b;

26 | P a g
e
27 | P a g
e
LAB-6
Add two arrays using friend function and operator
overloading.

CODE:
#include <iostream> using
namespace std; const int
MAX_SIZE = 100;

class Array {
private:
int size;
int elements[MAX_SIZE];

public:
Array(int s) : size(s) {
if (size > MAX_SIZE) {
cout << "Array size exceeds the maximum size." << endl; exit(1);
}

cout << "Enter elements for the array:" << endl; for
(int i = 0; i < size; ++i)
{
cin >> elements[i];

28 | P a g
e
}
}

friend Array operator+(const Array& arr1, const Array& arr2);

void display() const {


cout << "Array elements:" << endl; for
(int i = 0; i < size; ++i)
{
cout << elements[i] << " ";
}
cout << endl;
}
};

Array operator+(const Array& arr1, const Array& arr2)


{
if (arr1.size != arr2.size) {
cout << "Cannot add arrays with different sizes." << endl; exit(1);
}

int resultSize = arr1.size; Array


result(resultSize);

for (int i = 0; i < resultSize; ++i) {


result.elements[i] = arr1.elements[i] + arr2.elements[i];

29 | P a g
e
}

return result;
}

int main()
{
int size;

cout << "Enter the size of the arrays: "; cin


>> size;

Array array1(size);
Array array2(size);

Array sumArray = array1 + array2;

cout << "Sum of arrays:" << endl; sumArray.display();

return 0;
}
OUTPUT

30 | P a g
e
Add, subtract, multiply, and divide two complex numbers using
operator overloading (with Constructor).
CODE:

#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;

public:

Complex(double r, double i) : real(r), imag(i) {}

// Overload the addition operator (+)


Complex operator+(const Complex& other)
{
return Complex(real + other.real, imag + other.imag);
}

// Overload the subtraction operator (-)


Complex operator-(const Complex& other)
{
return Complex(real - other.real, imag - other.imag);
}
31 | P a g
e

// Overload the multiplication operator (*)


Complex operator*(const Complex& other)
{
return Complex(real * other.real - imag * other.imag, real *
other.imag + imag * other.real);
}

// Overload the division operator (/)


Complex operator/(const Complex& other)
{
double denominator = other.real * other.real + other.imag *
other.imag;
return Complex((real * other.real + imag * other.imag) /
denominator,
(imag * other.real - real * other.imag) /
denominator);
}

// Display the complex number


void display()
{
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex num1(4.0, 3.0);
Complex num2(5.0, 2.0);
32 | P a g
e
Complex sum = num1 + num2;
Complex diff = num1 - num2;
Complex product = num1 * num2;
Complex quotient = num1 / num2;

cout << "Num1: ";


num1.display();
cout << "Num2: ";
num2.display();
cout << "Sum: ";
sum.display();
cout << "Difference: ";
diff.display();
cout << "Product: ";
product.display();
cout << "Quotient: ";
quotient.display();

return 0;
}

OUTPUT

33 | P a g
e
Convert a square into a rectangle class (type conversion).
CODE
#include <iostream>
using namespace std;
class Square
{
protected:
double side;

public:
Square(double s) : side(s) {}

double getSide() const {


return side;
}

void setSide(double s) {
side = s;
}

double area() const {


return side * side;
}
};

34 | P a g
e
class Rectangle : public Square
{
private:
double width;

public:
Rectangle(double l, double w) : Square(l), width(w) {}

double getWidth() const {


return width;
}

void setWidth(double w) {
width = w;
}

double area() const {


return side * width;
}
};

int main()
{
Square square(5.0);
Rectangle rectangle(6.0, 4.0);

35 | P a g
e
cout << "Square Area: " << square.area() << " square units" <<
endl;
cout << "Rectangle Area: " << rectangle.area() << " square
units" << endl;

return 0;
}

OUTPUT

36 | P a g
e
LAB-7
Question:
Problem definition: A Bank maintains two kinds of account for
customers, one called a savings account and the other as current
account.

1. The savings account provides compound interest and withdrawal


faclities but no cheque book.
2. The current account provides withdrawal with cheque book but no
interest.

Create a class account that stores customer name, account number


and type of account. From this, derive the classes sav_acc and
cur_acc to make the more specific requirements. Include necessary
functions in order to achieve the following tasks:
a) Accept deposit from a customer and update the balance
b) Display the balance
c) Compute and deposit interest.
d) Permit withdrawal and update the balance

Do not use any constructors. Use member functions to initialize the


class members.

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

class Account {
protected:
37 string
|Pag
e
customerName; int
accountNumber;
double balance;

public:
void input() {
cout << "Enter Customer Name: ";
getline(cin, customerName);
cout << "Enter Account
Number: "; cin >>
accountNumber;
cout << "Enter Initial
Balance: "; cin >> balance;
cin.ignore(); // Clear the newline character from the buffer
}

void displayBalance() {
cout << "Account Number: " << accountNumber
<< endl; cout << "Customer Name: " <<
customerName << endl; cout << "Balance: " <<
balance << endl;
}

void deposit(double amount) {


if (amount > 0) {
balance += amount;
cout << "Deposit successful. New balance: " << balance << endl;

38 | P}aelse
g {
e
cout << "Invalid deposit amount. Please enter a positive amount."
<<
endl;
}
}
};

class Sav_Acc : public Account {


public:
void computeInterest() {
double interestRate = 0.05; // Adjust the interest rate as
needed double interest = balance * interestRate;
balance += interest;
cout << "Interest deposited. New balance: " << balance << endl;
}

void withdrawal(double amount) {


if (amount > 0 && amount <=
balance) { balance -= amount;
cout << "Withdrawal successful. New balance: " << balance <<
endl;
} else {
cout << "Invalid withdrawal amount or insufficient balance." <<
endl;
}
}
};

class Cur_Acc : public Account {


public:
39 | P a g
e
void withdrawalWithCheque(double
amount) { if (amount > 0 && amount
<= balance) {
balance -= amount;
cout << "Withdrawal with cheque successful. New balance: "
<< balance << endl;
} else {
cout << "Invalid withdrawal amount or insufficient balance." <<
endl;
}
}
};

int main() {
Sav_Acc savingsAccount;
Cur_Acc currentAccount;

cout << "Enter details for the Savings Account:" <<


endl; savingsAccount.input();

cout << "Enter details for the Current Account:" <<


endl; currentAccount.input();

int
choice;
do {

40 | P a g
e
cout << "\nChoose an
option:\n"; cout << "1.
Display Balance\n";
cout << "2. Deposit to Savings
Account\n"; cout << "3. Deposit to
Current Account\n";
cout << "4. Compute and Deposit
Interest\n"; cout << "5. Withdraw from
Savings Account\n";
cout << "6. Withdraw with Cheque from Current
Account\n"; cout << "7. Exit\n";
cin >> choice;

switch
(choice) {
case 1:
savingsAccount.displayBalanc
e();
currentAccount.displayBalanc
e(); break;
case 2:
double depositAmountSav;
cout << "Enter the amount to deposit in Savings
Account: "; cin >> depositAmountSav;
savingsAccount.deposit(depositAmount
41 | P a g Sav); break;
e
case 3:
double depositAmountCur;
cout << "Enter the amount to deposit in Current
Account: "; cin >> depositAmountCur;
currentAccount.deposit(depositAmountCur);
break;
case 4:
savingsAccount.computeInterest();
break;
case 5:
double withdrawalAmountSav;
cout << "Enter the amount to withdraw from Savings
Account: "; cin >> withdrawalAmountSav;
savingsAccount.withdrawal(withdrawalAmount
Sav); break;
case 6:
double withdrawalAmountCur;
cout << "Enter the amount to withdraw with cheque from
Current Account: ";
cin >> withdrawalAmountCur;
currentAccount.withdrawalWithCheque(withdrawalAmoun
tCur); break;
case 7:
cout << "Exiting program. Thank you!" << endl;
break;
default:
cout << "Invalid choice. Please select a valid option." << endl;
}
42 | P a g
e } while (choice != 7);
return 0;
}
OUTPUT:

43 | P a g
e
44 | P a g
e
45 | P a g
e
LAB-8
Program 1: Modify the program assignment of Lab 7 by using
constructors to initialize the class members in all 3 classes.
Code:
#include <iostream>
#include <string>
using namespace std;
class account
{
protected:
string customer_name;
int account_number;
float balance;

public:
account(const string &name, int acc_number) : customer_name(name),
account_number(acc_number), balance(0.0)
{
}
void acceptDeposit()
{
float amount;
cout << "Enter the amount to deposit: ";
cin >> amount;

balance += amount;
}
void displayBalance()
{
46 | P a g
e cout << "Account Number: " << account_number << endl;
cout << "Customer Name: " << customer_name << endl;
cout << "Balance: " << balance << endl;
}
};
class sav_acc : public account
{
public:
sav_acc(const string &name, int acc_number) : account(name, acc_number)
{
}
void computeInterest()
{

float rate, time;


cout << "Enter interest rate (in percentage): ";
cin >> rate;
cout << "Enter time (in years): ";
cin >> time;
float interest = (balance * rate * time) / 100;
balance += interest;
cout << "Interest of " << interest << " added to the account." << endl;
}
void withdraw()
{
float amount;
cout << "Enter the amount to withdraw: ";
cin >> amount;
if (amount <= balance)

{
47 | P a g
e balance -= amount;
cout << "Withdrawal of " << amount << " successful." << endl;
}

else
{
cout << "Insufficient balance for withdrawal." << endl;
}
}
};
class cur_acc : public account
{
public:
cur_acc(const string &name, int acc_number) : account(name, acc_number) {}
void withdraw()
{

float amount;
cout << "Enter the amount to withdraw: ";
cin >> amount;
if (amount <= balance)
{
balance -= amount;
cout << "Withdrawal of " << amount << " successful." << endl;
}
else
{
cout << "Insufficient balance for withdrawal." << endl;
}
}
};
48 | P a g
int
e main()
{
int choice;
cout << "1. Savings Account\n2. Current Account\nEnter your choice: ";
cin >> choice;

string customer_name;
int account_number;

cout << "Enter Customer Name: ";


cin >> customer_name;
cout << "Enter Account Number: ";
cin >> account_number;

if (choice == 1)
{
sav_acc sa(customer_name, account_number);
while (true)
{
cout << "1. Deposit\n2. Display Balance\n3. Compute Interest\n4. Withdraw\n5.
Exit\nEnter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
sa.acceptDeposit();
break;
case 2:
sa.displayBalance();
49 | P a gbreak;
e
case 3:
sa.computeInterest();
break;
case 4:
sa.withdraw();
break;
case 5:
return 0;
default:
cout << "Invalid choice. Try again." << endl;
}
}
}
else if (choice == 2)
{

cur_acc ca(customer_name, account_number);


while (true)
{

cout << "1. Deposit\n2. Display Balance\n3. Withdraw\n4. Exit\nEnter your choice: ";
cin >> choice;
switch (choice)
{
case 1:

ca.acceptDeposit();
break;
case 2:
ca.displayBalance();
break;
case 3:
50 | P a g
e ca.withdraw();
break;
case 4:
return 0;
default:
cout << "Invalid choice. Try again." << endl;
}
}
}
else
{
cout << "Invalid account type. Please choose 1 for Savings Account or 2 for Current
Account." << endl;
}
return 0;
}

OUTPUT:

51 | P a g
e
Program 2:
Code:
#include <iostream>
#include <string>
class Person
{ protected:
std::string name;
int code;
public:
Person(std::string _name, int _code) : name(_name), code(_code) {}
void displayPersonInfo()
{
std::cout << "Person Name: " << name << ", Code: " << code << std::endl;

}
}
;

class Account : public Person


{
protected:
double accountPay;

public:
Account(std::string _name, int _code, double _accountPay) : Person(_name, _code),
accountPay(_accountPay) {}
void displayAccountInfo()
{
52 | P a g
displayPersonInfo();
e
std::cout << "Account Pay: $" << accountPay << std::endl;
}
};
class Admin : public Person
{
protected:
int adminExperience;

public:
Admin(std::string _name, int _code, int _adminExperience) : Person(_name, _code),
adminExperience(_adminExperience) {}
void displayAdminInfo()
{
displayPersonInfo();
std::cout << "Admin Experience: " << adminExperience << " years" << std::endl;
}
};
class Master : public Account, public Admin
{
public:
Master(std::string _name, int _code, double _accountPay, int _adminExperience)

: Account(_name, _code, _accountPay), Admin(_name, _code, _adminExperience) {}


void displayMasterInfo()
{
std::cout << "Master Information:" << std::endl;
Account::displayPersonInfo();
std::cout << "Account Pay: $" << accountPay << std::endl;
std::cout << "Admin Experience: " << adminExperience << " years" << std::endl;
}
void updateMasterInfo(std::string _name, int _code, double _accountPay, int
_adminExperience)
53 | P a g
e {
Account::name = _name;
Account::code = _code;
accountPay = _accountPay;
adminExperience = _adminExperience;
}
};
int main()
{
std::string name;
int code, adminExperience;
double accountPay;
std::cout << "Enter Name: ";
std::cin >> name;
std::cout << "Enter Code: ";
std::cin >> code;

std::cout << "Enter Account Pay: $";


std::cin >> accountPay;
std::cout << "Enter Admin Experience (in years): ";
std::cin >> adminExperience;
Master master(name, code, accountPay, adminExperience);
std::cout << "\nMaster Information:" << std::endl;
master.displayMasterInfo();
return 0;
}

Output:

54 | P a g
e
LAB-9
Program 1: Write a C++ program to copy a given string into a
new string. Memory for the new string must be allocated
dynamically.

Code:
#include <iostream>
#include <cstring>
using namespace std; int
main() {
char inputString[100];

cout << "Enter a string: ";


cin.getline(inputString, 100); int length =
strlen(inputString); char* copiedString =
new char[length + 1]; strcpy(copiedString,
inputString);
cout << "Copied string: " << copiedString <<endl;
delete[] copiedString;

return 0;
}
OUTPUT :

55 | P a g
e
Program 2 : Write a C++program to sort an array in ascending order.
Code :
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;

int arr[n];

std::cout << "Enter the elements of the array: ";


for (int i = 0; i < n; i++) {
cin >> arr[i];
}

bubbleSort(arr, n);

std::cout << "Sorted array in ascending order: ";


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}

return 0;
}

OUTPUT :

56 | P a g
e
Lab-10
PROGRAM-1:
CODE
#include <iostream> #include
<fstream> using namespace
std;

int main()
{
char arr[100];
cout <<"Enter the name and age\n"; cin.getline (arr, 100);
ofstream myfile ("shailendra.txt"); myfile<<arr;
myfile.close();
cout << "File is created Successfully" <<endl <<endl; return 0;
}

OUTPUT

57 | P a g
e
PROGRAM-2:
CODE

#include<iostream>

#include<fstream>

using namespace std;

int main()

char arr[100];

cout<<"enter the name and age";

cin.getline (arr,100);

ofstream myfile("shukla.txt");

myfile<<arr;

myfile.close();

cout<<"file is created successfully"<<endl<<endl;

cout<<"file reading started !!!!"<<endl;

char arr1[100];

ifstream obj (" shukla.txt");

obj.getline (arr1,100);

cout<<arr1<<endl;

cout<<"file read operation completed"<<endl;

obj.close();

return 0;

} OUTPUT

58 | P a g
e
PROGRAM-3:
CODE

#include<iostream>

#include<fstream>

using namespace std;

int main()

char arr[100];

cout<<"enter the name and age";

cin.getline (arr,100);

ofstream myfile("shukla.text",ios::app);

myfile<<arr;

myfile.close();

cout<<"file is created successfully"<<endl<<endl;

cout<<"file reading started !!!!"<<endl;

char arr1[100];

ifstream obj (" shukla.text");

obj.getline (arr1,100);

cout<<arr1<<endl;

cout<<"file read operation completed"<<endl;

obj.close(); return 0; }

OUTPUT

59 | P a g
e
PROGRAM-4:
#include<iostream>

#include <fstream>

using namespace std;

int main()

ofstream fout;

fout.open("country");

fout<<"united states of america \n";

fout<<"united kingdom \n";

fout<<"south korea \n";

fout.close();

fout.open("capital");

fout<<"washington \n";

fout<<"london \n";

fout<<"seoul";

fout.close();

const int N=80;

char line[80];

ifstream fin;

fin.open("country");

60 | P a g
e
cout<<"contents of country file \n";

while(fin){

fin.getline(line,N);

cout <<line<<endl;

fin.close();

return 0;

OUTPUT

PROGRAM-5:
CODE

#include <fstream>

using namespace std;

int main()

ofstream fout;

fout.open("country");

fout<<"united states of america \n";

fout<<"united kingdom \n";

fout<<"south korea \n";

fout.close();
61 | P a g
e fout.open("capital");

fout<<"washington \n";

fout<<"london \n";
fout<<"seoul";

fout.close();

const int N=200;

char line[200];

ifstream fin;

fin.open("country");

cout<<"contents of country file \n";

while(!fin.eof()){

fin.getline(line,N);

cout <<line<<endl;

fin.close();

return 0;

OUTPUT

62 | P a g
e
PROGRAM-6:
#include<iostream>

#include<fstream>

using namespace std;

int main(){

ifstream f1,f2;
f1.open("country");

f2.open("capital");

char arr[100];

while(!f1.eof()||!f2.eof()){

f1.getline(arr,100);

cout<<"capital of"<<arr<<endl;

f2.getline(arr,100);

cout<<arr<<endl;

f1.close();

f2.close();

63 | P a g
e
Lab-11
PROGRAM-1:
Write a program with the following:
1. A function to read two "complex" double type numbers from keyboard.
2. A function to calculate division of these two complex numbers.
3. A try block to throw an exception when a wrong type of data is keyed in.
4. A try block to detect and throw an exception if the condition “divide by zero ” occurs.
5. Appropriate catch block to handle the exception thrown

CODE
#include <iostream>
#include <stdexcept>
using namespace std;
class Complex
{
public:
double real;
double imag;
};
// Function to read complex numbers from the keyboard Complex readComplexNumber()
{
Complex num;
cout << "Enter real part: ";
cin >> num.real;

if (cin.fail())
{
throw runtime_error("Invalid input. Real part must be a number.");
}

64cout
| P a<<
g "Enter imaginary part: ";
e
cin >> num.imag;
if (cin.fail())
{
throw runtime_error("Invalid input. Imaginary part must be a number.");
}

return num;
}

// Function to calculate the division of two complex numbers


Complex divideComplexNumbers(const Complex& num1, const Complex& num2)
{
Complex result;
double denominator = num2.real * num2.real + num2.imag * num2.imag;

if (denominator == 0)
{
throw runtime_error("Division by zero is not allowed.");
}

result.real = (num1.real * num2.real + num1.imag * num2.imag) / denominator;


result.imag = (num1.imag * num2.real - num1.real * num2.imag) / denominator;

return result;
}

int main()
{
try
{
65 | P a g
e
// Read two complex numbers
Complex num1 = readComplexNumber();
Complex num2 = readComplexNumber();

// Calculate the division of the two complex numbers


Complex result = divideComplexNumbers(num1, num2);

// Display the result


cout << "Result of division: " << result.real << " + " << result.imag << "i" << endl;
}
catch (const exception& e)
{
cerr << "Exception: " << e.what() << endl;

return 0;
}

OUTPUT

66 | P a g
e

You might also like