Professional Documents
Culture Documents
1f791c4c-cf95-4d8c-9faa-c0d0b6892a45-aiqq0e
1f791c4c-cf95-4d8c-9faa-c0d0b6892a45-aiqq0e
1f791c4c-cf95-4d8c-9faa-c0d0b6892a45-aiqq0e
Question -write a find out the total cost of shopping list with gst.
#include <vector>
#include <string>
class Item {
public:
std::string name;
double price;
int quantity;
};
class ShoppingList {
private:
std::vector<Item> items;
double gstRate;
public:
return totalCostWithGST;
std::cout << item.name << " - " << item.quantity << " x $" << item.price << std::endl;
1
}
};
int main() {
double gstRate;
ShoppingList shoppingList(gstRate);
int numberOfItems;
std::string name;
double price;
int quantity;
shoppingList.printShoppingList();
std::cout << "Total cost including GST: $" << totalCostWithGST << std::endl;
return 0;
2
Output –
Shopping List:
mango - 3 x $19.5
milk - 2 x $7.5
pen - 1 x $3
3
Assignment – 3
Question - write a c++ program to print sum of n no. of natural number by using friend function.
class NaturalNumberSum {
private:
int n;
public:
};
int sum = 0;
sum += i;
return sum;
int main() {
int number;
NaturalNumberSum naturalNumberSum(number);
std::cout << "The sum of the first " << number << " natural numbers is: " << sum << std::endl;
return 0;
Output –
Question – write a c++ program to count the number of occurrences of a given number in a sorted array of integers.
#include <vector>
class NumberCounter {
private:
4
std::vector<int> sortedArray;
public:
private:
int count = 0;
return count;
if (sortedArray[mid] == number) {
result = mid;
right = mid - 1;
right = mid - 1;
} else {
left = mid + 1;
return result;
5
int result = -1;
if (sortedArray[mid] == number) {
result = mid;
left = mid + 1;
right = mid - 1;
} else {
left = mid + 1;
return result;
};
int main() {
std::vector<int> sortedArray;
sortedArray.push_back(number);
NumberCounter counter(sortedArray);
std::cout << "The number " << searchNumber << " occurs " << occurrences << " times in the array.\n";
return 0;
Output –
6
Enter the elements of the sorted array:
Question – write a c++ program to find a missing element of two given array of integer except 1 integer.
#include <vector>
#include <unordered_map>
class MissingElementFinder {
public:
elementCount[num]++;
elementCount[num]--;
if (pair.second == 1) {
return pair.first;
return -1;
};
int main() {
7
std::cout << "Enter the elements of the first array:\n";
array1.push_back(number);
array2.push_back(number);
MissingElementFinder finder;
if (missingElement != -1) {
std::cout << "The missing element is: " << missingElement << std::endl;
} else {
std::cout << "No missing element found (unexpected case)." << std::endl;
return 0;
Output –
8
Assignment – 4
Question – write a c++ program to rearrange a given sorted array of positive integer. Note the final array of first element
should b maximum value, second maximum value, third second maximum value, fouth second maximum value, fifth
third maximum value and so on.
#include <vector>
class ArrayRearranger {
public:
std::vector<int> rearrangedArray;
int n = sortedArray.size();
if (left != right) {
rearrangedArray.push_back(sortedArray[right--]);
rearrangedArray.push_back(sortedArray[left++]);
} else {
rearrangedArray.push_back(sortedArray[left++]);
return rearrangedArray;
};
int main() {
std::vector<int> sortedArray;
sortedArray.push_back(number);
ArrayRearranger rearranger;
9
std::cout << "Rearranged array: ";
return 0;
Output –
Rearranged array: 10 0 8 1 7 3 6 4 5
Question – write a c++ program to move all negative element of an array of negative integer to the end of the array.
This is done without changing the order of positive and negative element of the array.
#include <vector>
class ArrayProcessor {
public:
int n = arr.size();
std::vector<int> result(n);
if (arr[i] >= 0) {
result[posIndex++] = arr[i];
} else {
result[negIndex--] = arr[i];
if (i < posIndex) {
arr[i] = result[i];
} else {
10
}
};
int main() {
std::vector<int> array;
array.push_back(number);
ArrayProcessor processor;
processor.printArray(array);
processor.moveNegativesToEnd(array);
std::cout << "Array after moving negative elements to the end: ";
processor.printArray(array);
return 0;
Output –
Question – write a c++ program to find and print all the common elements in 3 sorted array integers.
#include <vector>
class CommonElementsFinder {
public:
11
std::vector<int> findCommonElements(const std::vector<int>& arr1, const std::vector<int>& arr2, const
std::vector<int>& arr3) {
std::vector<int> commonElements;
int i = 0, j = 0, k = 0;
commonElements.push_back(arr1[i]);
i++;
j++;
k++;
i++;
j++;
} else {
k++;
return commonElements;
};
int main() {
std::cout << "Enter the size of the first sorted array: ";
12
std::cin >> number;
arr1.push_back(number);
std::cout << "Enter the size of the second sorted array: ";
arr2.push_back(number);
std::cout << "Enter the size of the third sorted array: ";
arr3.push_back(number);
CommonElementsFinder finder;
finder.printArray(commonElements);
return 0;
Output –
Question- write a c++ program to find the no. of pairs of integers in a given array of integer whose sum is equal to a
specified number.
13
#include <vector>
#include <unordered_map>
class PairFinder {
public:
int pairCount = 0;
if (numCount[complement] > 0) {
pairCount += numCount[complement];
numCount[num]++;
return pairCount;
};
int main() {
std::vector<int> arr;
arr.push_back(number);
PairFinder finder;
std::cout << "The number of pairs with sum " << targetSum << " is: " << pairCount << std::endl;
return 0;
14
Output –
15
Assignment – 5
Question - Create a class name rectangle with two data members length, breadth and a function two calculate the area
which is length into breadth.
#include <iostream>
class Rectangle {
private:
double length;
double breadth;
public:
Rectangle() {
length = 0.0;
breadth = 0.0;
Rectangle(double l, double b) {
length = l;
breadth = b;
if (choice == 'l') {
length = value;
breadth = 0.0;
length = 0.0;
breadth = value;
} else {
length = 0.0;
breadth = 0.0;
16
double calculateArea() {
};
int main() {
Rectangle rect1;
return 0;
Output –
Area of rectangle 1: 0
Area of rectangle 2: 15
Area of rectangle 3: 0
Question - suppose you have a piggi bank with an amount of 1000/- and you have to add some more amount to it.
Create a class add amount, with data member name, amount with an initial value thousand
II) having a parameter which is the amount that will be added to the piggi bank
III)Create an object of the addamout class and display the final amount in piggi bank
#include <string>
class AddAmount {
private:
string name;
double amount;
public:
AddAmount() {
17
amount = 1000.0;
AddAmount(double addedAmount) {
void displayAmount() {
cout << "Final amount in " << name << ": " << amount << endl;
};
int main() {
AddAmount piggyBank1;
piggyBank1.displayAmount();
AddAmount piggyBank2(500.0);
piggyBank2.displayAmount();
return 0;
Output –
Question - Create a class student with 3 data members are name, age, address. The constructor of the class assigned
default values to name will be unknown, age will be 0 address will be non available
#include <string>
class Student {
private:
std::string name;
int age;
std::string address;
public:
Student() {
18
name = "Unknown";
age = 0;
name = studentName;
age = studentAge;
name = studentName;
age = studentAge;
address = studentAddress;
name = studentName;
age = studentAge;
name = studentName;
age = studentAge;
address = studentAddress;
void printInfo() {
std::cout << "Name: " << name << ", Age: " << age << ", Address: " << address << std::endl;
};
int main() {
Student students[10];
19
students[5].setInfo("Frank", 25, "987 Cedar St");
students[i].printInfo();
return 0;
Output –
Information of 10 students:
Question – A boy has his money deposited 1000 ,1500 , 2000 in the bank a b c we have to print the money deposited
by him in a particular bank create a class bank with return 0
3 Cub class
Bank A
Bank B
Bank C
which return the amount in particular bank call the function with balance by the object of three banks
class Bank {
public:
20
};
public:
return 1000;
};
public:
return 1500;
};
public:
return 2000;
};
int main() {
BankA bankA;
BankB bankB;
BankC bankC;
std::cout << "Amount deposited in Bank A: " << bankA.getBalance() << std::endl;
std::cout << "Amount deposited in Bank B: " << bankB.getBalance() << std::endl;
std::cout << "Amount deposited in Bank C: " << bankC.getBalance() << std::endl;
return 0;
Output –
Amount deposited in Bank A: 1000, Amount deposited in Bank B: 1500, Amount deposited in Bank C: 2000
21
Assignment – 7
Question - C++ program to add two objects using binary plus operator overloading.
class Complex {
private:
double real;
double imag;
public:
Complex temp;
return temp;
cout << real << " + " << imag << "i" << endl;
};
int main() {
Complex c3 = c1 + c2;
c1.display();
c2.display();
c3.display();
return 0;
22
Output –
c1 = 3.5 + 2.5i
c2 = 1.5 + 3.5i
c1 + c2 = 5 + 6i
class Counter {
private:
int value;
public:
Counter(int v = 0) : value(v) {}
Counter& operator++() {
++value;
return *this;
};
int main() {
Counter c1(5);
c1.display();
++c1;
c1.display();
return 0;
Output –
Question - Make a class name fruit with a data member to calculate a number of fruits in the basket. Create two other
class name apple and mango to calculate the no. of apples and mangoes in the basket. Print the no. of fruits of each
type and the total no. of fruits in the basket.
23
Ans - #include <iostream>
class Fruit {
protected:
int count;
public:
Fruit(int c = 0) : count(c) {}
return count;
};
public:
Apple(int c = 0) : Fruit(c) {}
};
public:
Mango(int c = 0) : Fruit(c) {}
};
int main() {
Apple apples(10);
Mango mangoes(20);
apples.display();
mangoes.display();
cout << "Total number of fruits: " << totalFruits << endl;
return 0;
24
Output –
Number of apples: 10
Number of mangoes: 20
Question - Assume that a bank maintains two kinds of accounts for customers, one called as savings account and the
other as current account. The savings account provides compound interest and withdrawal facilities but no cheque
book facility. The current account provides cheque book facility but no interest. Current account holders should also
maintain a minimum balance and if the balance falls below this level, a service charge is imposed. Create a class account
that stores customer name, account number and type of account. From this derive the classes cur_acct and sav_acct
to make them more specific to their requirements. Include necessary member functions to achieve the following tasks:
(e) Check for the minimum balance, impose penalty, necessary, and update the balance.
Do not use any constructors. Use member functions to initialize the class members.
#include <string>
#include <cmath>
class Account {
protected:
string customerName;
int accountNumber;
string accountType;
double balance;
public:
customerName = name;
accountNumber = accNum;
accountType = accType;
balance = initialBalance;
if (amount > 0) {
balance += amount;
25
cout << "Deposited: $" << amount << ". New balance: $" << balance << endl;
} else {
};
private:
double interestRate;
public:
interestRate = rate;
deposit(interest);
cout << "Interest computed and deposited: $" << interest << ". New balance: $" << balance << endl;
balance -= amount;
cout << "Withdrew: $" << amount << ". New balance: $" << balance << endl;
} else {
};
private:
double minimumBalance;
double serviceCharge;
26
public:
minimumBalance = minBal;
serviceCharge = charge;
balance -= amount;
cout << "Withdrew: $" << amount << ". New balance: $" << balance << endl;
checkMinimumBalance();
} else {
void checkMinimumBalance() {
balance -= serviceCharge;
cout << "Balance fell below minimum. Service charge imposed: $" << serviceCharge << ". New balance: $" <<
balance << endl;
};
int main() {
SavAcct savingsAccount;
savingsAccount.setInterestRate(5.0);
savingsAccount.deposit(500.00);
savingsAccount.computeAndDepositInterest(1);
savingsAccount.withdraw(200.00);
savingsAccount.displayBalance();
CurAcct currentAccount;
currentAccount.setMinimumBalance(1000.00);
27
currentAccount.setServiceCharge(50.00);
currentAccount.deposit(500.00);
currentAccount.withdraw(1000.00);
currentAccount.withdraw(1000.00);
currentAccount.displayBalance();
return 0;
Output –
Balance: $1375
Balance fell below minimum. Service charge imposed: $50. New balance: $-50
Balance: $-50
28
Assignment 1
Ans
#include <iostream>
int main() {
return 0;
Q - c ++ program to find the area of the triangle using heron's formula using user input
Ans
#include <iostream>
#include <cmath>
29
int main() {
cout << "Enter the lengths of the three sides of the triangle: ";
cout << "Area of the triangle = " << area << endl;
return 0;
Ans
#include <iostream>
int main() {
cout << "Enter the length of the side of the square: ";
30
// Display the result
cout << "Area of the square = " << area << endl;
return 0;
31
Assignment 2
Ans
#include <iostream>
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
int main() {
32
// Calculate the LCM of three numbers
cout << "LCM of " << num1 << ", " << num2 << ", and " << num3 << " is " << result << endl;
return 0;
Ans
#include <iostream>
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
int main() {
33
cin >> num1 >> num2 >> num3;
cout << "GCD of " << num1 << ", " << num2 << ", and " << num3 << " is " << result << endl;
return 0;
Ans
#include <iostream>
int main() {
while (num != 0) {
num /= 10;
return 0;
34
Q - c++ program to calculate the power of a number using user input
Ans
#include <iostream>
#include <cmath>
int main() {
cout << base << " raised to the power of " << exponent << " is " << result << endl;
return 0;
35