1f791c4c-cf95-4d8c-9faa-c0d0b6892a45-aiqq0e

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Assignment – 6

Question -write a find out the total cost of shopping list with gst.

Ans - #include <iostream>

#include <vector>

#include <string>

class Item {

public:

std::string name;

double price;

int quantity;

Item(const std::string& itemName, double itemPrice, int itemQuantity)

: name(itemName), price(itemPrice), quantity(itemQuantity) {}

};

class ShoppingList {

private:

std::vector<Item> items;

double gstRate;

public:

ShoppingList(double gst) : gstRate(gst) {}

void addItem(const std::string& name, double price, int quantity) {

items.emplace_back(name, price, quantity);

double calculateTotalCost() const {

double totalCost = 0.0;

for (const auto& item : items) {

totalCost += item.price * item.quantity;

double totalCostWithGST = totalCost + (totalCost * gstRate / 100);

return totalCostWithGST;

void printShoppingList() const {

std::cout << "Shopping List:" << std::endl;

for (const auto& item : items) {

std::cout << item.name << " - " << item.quantity << " x $" << item.price << std::endl;

1
}

};

int main() {

double gstRate;

std::cout << "Enter GST rate (in %): ";

std::cin >> gstRate;

ShoppingList shoppingList(gstRate);

int numberOfItems;

std::cout << "Enter the number of items: ";

std::cin >> numberOfItems;

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

std::string name;

double price;

int quantity;

std::cout << "Enter item name: ";

std::cin >> name;

std::cout << "Enter item price: ";

std::cin >> price;

std::cout << "Enter item quantity: ";

std::cin >> quantity;

shoppingList.addItem(name, price, quantity);

shoppingList.printShoppingList();

double totalCostWithGST = shoppingList.calculateTotalCost();

std::cout << "Total cost including GST: $" << totalCostWithGST << std::endl;

return 0;

2
Output –

Enter GST rate (in %): 6

Enter the number of items: 3

Enter item name: mango

Enter item price: 19.50

Enter item quantity: 3

Enter item name: milk

Enter item price: 7.50

Enter item quantity: 2

Enter item name: pen

Enter item price: 3.00

Enter item quantity: 1

Shopping List:

mango - 3 x $19.5

milk - 2 x $7.5

pen - 1 x $3

Total cost including GST: $81.09

3
Assignment – 3

Question - write a c++ program to print sum of n no. of natural number by using friend function.

Ans – #include <iostream>

class NaturalNumberSum {

private:

int n;

public:

NaturalNumberSum(int number) : n(number) {}

friend int calculateSum(const NaturalNumberSum& obj);

};

int calculateSum(const NaturalNumberSum& obj) {

int sum = 0;

for (int i = 1; i <= obj.n; ++i) {

sum += i;

return sum;

int main() {

int number;

std::cout << "Enter the number of natural numbers to sum: ";

std::cin >> number;

NaturalNumberSum naturalNumberSum(number);

int sum = calculateSum(naturalNumberSum);

std::cout << "The sum of the first " << number << " natural numbers is: " << sum << std::endl;

return 0;

Output –

Enter the number of natural numbers to sum: 4

The sum of the first 4 natural numbers is: 10

Question – write a c++ program to count the number of occurrences of a given number in a sorted array of integers.

Ans - #include <iostream>

#include <vector>

class NumberCounter {

private:

4
std::vector<int> sortedArray;

public:

NumberCounter(const std::vector<int>& array) : sortedArray(array) {}

int countOccurrences(int number) {

return countOccurrencesHelper(number, 0, sortedArray.size() - 1);

private:

int countOccurrencesHelper(int number, int left, int right) {

int count = 0;

// Find the first occurrence of the number

int first = findFirstOccurrence(number, left, right);

// Find the last occurrence of the number

int last = findLastOccurrence(number, left, right);

if (first != -1 && last != -1) {

count = last - first + 1;

return count;

int findFirstOccurrence(int number, int left, int right) {

int result = -1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (sortedArray[mid] == number) {

result = mid;

right = mid - 1;

} else if (sortedArray[mid] > number) {

right = mid - 1;

} else {

left = mid + 1;

return result;

int findLastOccurrence(int number, int left, int right) {

5
int result = -1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (sortedArray[mid] == number) {

result = mid;

left = mid + 1;

} else if (sortedArray[mid] > number) {

right = mid - 1;

} else {

left = mid + 1;

return result;

};

int main() {

std::vector<int> sortedArray;

int size, number, searchNumber;

std::cout << "Enter the size of the sorted array: ";

std::cin >> size;

std::cout << "Enter the elements of the sorted array:\n";

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

std::cin >> number;

sortedArray.push_back(number);

std::cout << "Enter the number to count its occurrences: ";

std::cin >> searchNumber;

NumberCounter counter(sortedArray);

int occurrences = counter.countOccurrences(searchNumber);

std::cout << "The number " << searchNumber << " occurs " << occurrences << " times in the array.\n";

return 0;

Output –

Enter the size of the sorted array: 4

6
Enter the elements of the sorted array:

Enter the number to count its occurrences: 2

The number 2 occurs 2 times in the array.

Question – write a c++ program to find a missing element of two given array of integer except 1 integer.

Ans - #include <iostream>

#include <vector>

#include <unordered_map>

class MissingElementFinder {

public:

int findMissingElement(const std::vector<int>& array1, const std::vector<int>& array2) {

std::unordered_map<int, int> elementCount;

for (int num : array1) {

elementCount[num]++;

for (int num : array2) {

elementCount[num]--;

for (const auto& pair : elementCount) {

if (pair.second == 1) {

return pair.first;

return -1;

};

int main() {

std::vector<int> array1, array2;

int size1, size2, number;

std::cout << "Enter the size of the first array: ";

std::cin >> size1;

7
std::cout << "Enter the elements of the first array:\n";

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

std::cin >> number;

array1.push_back(number);

std::cout << "Enter the size of the second array: ";

std::cin >> size2;

std::cout << "Enter the elements of the second array:\n";

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

std::cin >> number;

array2.push_back(number);

MissingElementFinder finder;

int missingElement = finder.findMissingElement(array1, array2);

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 –

Enter the size of the first array: 4

Enter the elements of the first array: 1

Enter the size of the second array: 4

Enter the elements of the second array:

The missing element is: 4

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.

Ans - #include <iostream>

#include <vector>

class ArrayRearranger {

public:

std::vector<int> rearrangeArray(const std::vector<int>& sortedArray) {

std::vector<int> rearrangedArray;

int n = sortedArray.size();

int left = 0, right = n - 1;

while (left <= right) {

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;

int size, number;

std::cout << "Enter the size of the sorted array: ";

std::cin >> size;

std::cout << "Enter the elements of the sorted array:\n";

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

std::cin >> number;

sortedArray.push_back(number);

ArrayRearranger rearranger;

std::vector<int> rearrangedArray = rearranger.rearrangeArray(sortedArray);

9
std::cout << "Rearranged array: ";

for (int num : rearrangedArray) {

std::cout << num << " ";

std::cout << std::endl;

return 0;

Output –

Enter the size of the sorted array: 9

Enter the elements of the sorted array:0 1 3 4 5 6 7 8 10

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.

Ans- #include <iostream>

#include <vector>

class ArrayProcessor {

public:

void moveNegativesToEnd(std::vector<int>& arr) {

int n = arr.size();

std::vector<int> result(n);

int posIndex = 0, negIndex = n - 1;

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

if (arr[i] >= 0) {

result[posIndex++] = arr[i];

} else {

result[negIndex--] = arr[i];

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

if (i < posIndex) {

arr[i] = result[i];

} else {

arr[i] = result[n - (i - posIndex) - 1];

10
}

void printArray(const std::vector<int>& arr) {

for (int num : arr) {

std::cout << num << " ";

std::cout << std::endl;

};

int main() {

std::vector<int> array;

int size, number;

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

std::cin >> size;

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

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

std::cin >> number;

array.push_back(number);

ArrayProcessor processor;

std::cout << "Original array: ";

processor.printArray(array);

processor.moveNegativesToEnd(array);

std::cout << "Array after moving negative elements to the end: ";

processor.printArray(array);

return 0;

Output –

Original array: 0 9 -7 2 -12 11 -20

Array after moving negative elements to the end: 0 9 2 11 -7 -12 -20

Question – write a c++ program to find and print all the common elements in 3 sorted array integers.

Ans - #include <iostream>

#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;

while (i < arr1.size() && j < arr2.size() && k < arr3.size()) {

if (arr1[i] == arr2[j] && arr2[j] == arr3[k]) {

commonElements.push_back(arr1[i]);

i++;

j++;

k++;

else if (arr1[i] < arr2[j]) {

i++;

} else if (arr2[j] < arr3[k]) {

j++;

} else {

k++;

return commonElements;

void printArray(const std::vector<int>& arr) {

for (int num : arr) {

std::cout << num << " ";

std::cout << std::endl;

};

int main() {

std::vector<int> arr1, arr2, arr3;

int size1, size2, size3, number;

std::cout << "Enter the size of the first sorted array: ";

std::cin >> size1;

std::cout << "Enter the elements of the first sorted array:\n";

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

12
std::cin >> number;

arr1.push_back(number);

std::cout << "Enter the size of the second sorted array: ";

std::cin >> size2;

std::cout << "Enter the elements of the second sorted array:\n";

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

std::cin >> number;

arr2.push_back(number);

std::cout << "Enter the size of the third sorted array: ";

std::cin >> size3;

std::cout << "Enter the elements of the third sorted array:\n";

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

std::cin >> number;

arr3.push_back(number);

CommonElementsFinder finder;

std::vector<int> commonElements = finder.findCommonElements(arr1, arr2, arr3);

std::cout << "Common elements in the three arrays: ";

finder.printArray(commonElements);

return 0;

Output –

Enter the size of the first sorted array: 6

Enter the elements of the first sorted array: 1 5 7 8 9 11

Enter the size of the second sorted array: 6

Enter the elements of the second sorted array: 6 8 10 11 12 16

Enter the size of the third sorted array: 8

Enter the elements of the third sorted array: 1 3 5 6 8 10 11 17

Common elements in the three arrays: 8 11

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.

Ans - #include <iostream>

13
#include <vector>

#include <unordered_map>

class PairFinder {

public:

int countPairsWithSum(const std::vector<int>& arr, int targetSum) {

std::unordered_map<int, int> numCount;

int pairCount = 0;

for (int num : arr) {

int complement = targetSum - num;

if (numCount[complement] > 0) {

pairCount += numCount[complement];

numCount[num]++;

return pairCount;

};

int main() {

std::vector<int> arr;

int size, number, targetSum;

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

std::cin >> size;

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

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

std::cin >> number;

arr.push_back(number);

std::cout << "Enter the target sum: ";

std::cin >> targetSum;

PairFinder finder;

int pairCount = finder.countPairsWithSum(arr, targetSum);

std::cout << "The number of pairs with sum " << targetSum << " is: " << pairCount << std::endl;

return 0;

14
Output –

Enter the size of the array: 8

Enter the elements of the array:1 5 7 5 8 9 11 12

Enter the target sum: 12

The number of pairs with sum 12 is: 3

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.

The class has preconstructed

I) having no parameter value

II)having 2 parameter length and breadth

III) only 1 parameter length or breadth

#include <iostream>

using namespace std;

class Rectangle {

private:

double length;

double breadth;

public:

Rectangle() {

length = 0.0;

breadth = 0.0;

Rectangle(double l, double b) {

length = l;

breadth = b;

Rectangle(double value, char choice) {

if (choice == 'l') {

length = value;

breadth = 0.0;

} else if (choice == 'b') {

length = 0.0;

breadth = value;

} else {

cout << "Invalid choice for constructor!" << endl;

length = 0.0;

breadth = 0.0;

16
double calculateArea() {

return length * breadth;

};

int main() {

Rectangle rect1;

cout << "Area of rectangle 1: " << rect1.calculateArea() << endl;

Rectangle rect2(5.0, 3.0);

cout << "Area of rectangle 2: " << rect2.calculateArea() << endl;

Rectangle rect3(4.0, 'l');

cout << "Area of rectangle 3: " << rect3.calculateArea() << endl;

Rectangle rect4(6.0, 'b');

cout << "Area of rectangle 4: " << rect4.calculateArea() << endl;

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

Now make a 2 constructor of this class

I) without any parameter

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

Ans - #include <iostream>

#include <string>

using namespace std;

class AddAmount {

private:

string name;

double amount;

public:

AddAmount() {

name = "Piggy Bank";

17
amount = 1000.0;

AddAmount(double addedAmount) {

name = "Piggy Bank";

amount = 1000.0 + 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 –

Final amount in Piggy Bank: 1000

Final amount in Piggy Bank: 1500

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

It has 2 function to the same name same info

I) having 2 parameters name, age

II)having 3 parameters name, age, address

Now print the name, age, address of 10 students

Ans - #include <iostream>

#include <string>

class Student {

private:

std::string name;

int age;

std::string address;

public:

Student() {

18
name = "Unknown";

age = 0;

address = "Not available";

Student(std::string studentName, int studentAge) {

name = studentName;

age = studentAge;

address = "Not available";

Student(std::string studentName, int studentAge, std::string studentAddress) {

name = studentName;

age = studentAge;

address = studentAddress;

void setInfo(std::string studentName, int studentAge) {

name = studentName;

age = studentAge;

void setInfo(std::string studentName, int studentAge, std::string studentAddress) {

name = studentName;

age = studentAge;

address = studentAddress;

void printInfo() {

std::cout << "Name: " << name << ", Age: " << age << ", Address: " << address << std::endl;

};

int main() {

Student students[10];

students[0].setInfo("Alice", 20, "123 Main St");

students[1].setInfo("Bob", 22, "456 Elm St");

students[2].setInfo("Charlie", 21, "789 Oak St");

students[3].setInfo("David", 23, "321 Pine St");

students[4].setInfo("Eva", 19, "654 Maple St");

19
students[5].setInfo("Frank", 25, "987 Cedar St");

students[6].setInfo("Grace", 18, "135 Birch St");

students[7].setInfo("Hannah", 24, "246 Walnut St");

students[8].setInfo("Ian", 22, "579 Spruce St");

students[9].setInfo("Jenny", 20, "802 Fir St");

std::cout << "Information of 10 students:\n";

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

std::cout << "Student " << i + 1 << ": ";

students[i].printInfo();

return 0;

Output –

Information of 10 students:

Student 1: Name: Alice, Age: 20, Address: 123 Main St

Student 2: Name: Bob, Age: 22, Address: 456 Elm St

Student 3: Name: Charlie, Age: 21, Address: 789 Oak St

Student 4: Name: David, Age: 23, Address: 321 Pine St

Student 5: Name: Eva, Age: 19, Address: 654 Maple St

Student 6: Name: Frank, Age: 25, Address: 987 Cedar St

Student 7: Name: Grace, Age: 18, Address: 135 Birch St

Student 8: Name: Hannah, Age: 24, Address: 246 Walnut St

Student 9: Name: Ian, Age: 22, Address: 579 Spruce St

Student 10: Name: Jenny, Age: 20, Address: 802 Fir St

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

Ans - #include <iostream>

class Bank {

public:

virtual int getBalance() = 0;

20
};

class BankA : public Bank {

public:

int getBalance() override {

return 1000;

};

class BankB : public Bank {

public:

int getBalance() override {

return 1500;

};

class BankC : public Bank {

public:

int getBalance() override {

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.

Ans - #include <iostream>

using namespace std;

class Complex {

private:

double real;

double imag;

public:

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

Complex operator + (const Complex& obj) {

Complex temp;

temp.real = real + obj.real;

temp.imag = imag + obj.imag;

return temp;

void display() const {

cout << real << " + " << imag << "i" << endl;

};

int main() {

Complex c1(3.5, 2.5);

Complex c2(1.5, 3.5);

Complex c3 = c1 + c2;

cout << "c1 = ";

c1.display();

cout << "c2 = ";

c2.display();

cout << "c1 + c2 = ";

c3.display();

return 0;

22
Output –

c1 = 3.5 + 2.5i

c2 = 1.5 + 3.5i

c1 + c2 = 5 + 6i

Question - C++ program to overload a prefix increment operator.

Ans - #include <iostream>

using namespace std;

class Counter {

private:

int value;

public:

Counter(int v = 0) : value(v) {}

Counter& operator++() {

++value;

return *this;

void display() const {

cout << "Counter value: " << value << endl;

};

int main() {

Counter c1(5);

cout << "Initial ";

c1.display();

++c1;

cout << "After prefix increment ";

c1.display();

return 0;

Output –

Initial Counter value: 5

After prefix increment Counter value: 6

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>

using namespace std;

class Fruit {

protected:

int count;

public:

Fruit(int c = 0) : count(c) {}

int getCount() const {

return count;

};

class Apple : public Fruit {

public:

Apple(int c = 0) : Fruit(c) {}

void display() const {

cout << "Number of apples: " << count << endl;

};

class Mango : public Fruit {

public:

Mango(int c = 0) : Fruit(c) {}

void display() const {

cout << "Number of mangoes: " << count << endl;

};

int main() {

Apple apples(10);

Mango mangoes(20);

apples.display();

mangoes.display();

int totalFruits = apples.getCount() + mangoes.getCount();

cout << "Total number of fruits: " << totalFruits << endl;

return 0;

24
Output –

Number of apples: 10

Number of mangoes: 20

Total number of fruits: 30

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:

(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.

(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.

Ans- #include <iostream>

#include <string>

#include <cmath>

using namespace std;

class Account {

protected:

string customerName;

int accountNumber;

string accountType;

double balance;

public:

void initialize(string name, int accNum, string accType, double initialBalance) {

customerName = name;

accountNumber = accNum;

accountType = accType;

balance = initialBalance;

void deposit(double amount) {

if (amount > 0) {

balance += amount;

25
cout << "Deposited: $" << amount << ". New balance: $" << balance << endl;

} else {

cout << "Invalid deposit amount." << endl;

void displayBalance() const {

cout << "Balance: $" << balance << endl;

};

class SavAcct : public Account {

private:

double interestRate;

public:

void setInterestRate(double rate) {

interestRate = rate;

void computeAndDepositInterest(int time) {

double interest = balance * pow((1 + interestRate / 100), time) - balance;

deposit(interest);

cout << "Interest computed and deposited: $" << interest << ". New balance: $" << balance << endl;

void withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

cout << "Withdrew: $" << amount << ". New balance: $" << balance << endl;

} else {

cout << "Invalid or insufficient funds for withdrawal." << endl;

};

class CurAcct : public Account {

private:

double minimumBalance;

double serviceCharge;

26
public:

void setMinimumBalance(double minBal) {

minimumBalance = minBal;

void setServiceCharge(double charge) {

serviceCharge = charge;

void withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

cout << "Withdrew: $" << amount << ". New balance: $" << balance << endl;

checkMinimumBalance();

} else {

cout << "Invalid or insufficient funds for withdrawal." << endl;

void checkMinimumBalance() {

if (balance < minimumBalance) {

balance -= serviceCharge;

cout << "Balance fell below minimum. Service charge imposed: $" << serviceCharge << ". New balance: $" <<
balance << endl;

};

int main() {

SavAcct savingsAccount;

savingsAccount.initialize("Alice Smith", 1001, "Savings", 1000.00);

savingsAccount.setInterestRate(5.0);

savingsAccount.deposit(500.00);

savingsAccount.computeAndDepositInterest(1);

savingsAccount.withdraw(200.00);

savingsAccount.displayBalance();

CurAcct currentAccount;

currentAccount.initialize("Bob Johnson", 2001, "Current", 1500.00);

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 –

Deposited: $500. New balance: $1500

Deposited: $75. New balance: $1575

Interest computed and deposited: $75. New balance: $1575

Withdrew: $200. New balance: $1375

Balance: $1375

Deposited: $500. New balance: $2000

Withdrew: $1000. New balance: $1000

Withdrew: $1000. New balance: $0

Balance fell below minimum. Service charge imposed: $50. New balance: $-50

Balance: $-50

28
Assignment 1

Q - c++ program to find the simple interest using user input

Ans

#include <iostream>

using namespace std;

int main() {

float principal, rate, time, simpleInterest;

// Input principal amount

cout << "Enter the principal amount: ";

cin >> principal;

// Input rate of interest

cout << "Enter the rate of interest (in percentage): ";

cin >> rate;

// Input time period

cout << "Enter the time period (in years): ";

cin >> time;

// Calculate simple interest

simpleInterest = (principal * rate * time) / 100;

// Display the result

cout << "Simple Interest = " << simpleInterest << endl;

return 0;

Q - c ++ program to find the area of the triangle using heron's formula using user input

Ans

#include <iostream>

#include <cmath>

using namespace std;

29
int main() {

float side1, side2, side3;

float semiPerimeter, area;

// Input the lengths of the sides

cout << "Enter the lengths of the three sides of the triangle: ";

cin >> side1 >> side2 >> side3;

// Calculate the semi-perimeter of the triangle

semiPerimeter = (side1 + side2 + side3) / 2;

// Calculate the area using Heron's formula

area = sqrt(semiPerimeter * (semiPerimeter - side1) * (semiPerimeter - side2) * (semiPerimeter - side3));

// Display the result

cout << "Area of the triangle = " << area << endl;

return 0;

Q - c++ program to find the area of a square using user input

Ans

#include <iostream>

using namespace std;

int main() {

float side, area;

// Input the length of the side

cout << "Enter the length of the side of the square: ";

cin >> side;

// Calculate the area of the square

area = side * side;

30
// Display the result

cout << "Area of the square = " << area << endl;

return 0;

31
Assignment 2

Q - c ++ program to find lcm of 3 number using user input

Ans

#include <iostream>

using namespace std;

// Function to find the GCD of two numbers

int gcd(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

// Function to find the LCM of two numbers

int lcm(int a, int b) {

return (a * b) / gcd(a, b);

// Function to find the LCM of three numbers

int lcmThreeNumbers(int a, int b, int c) {

// LCM(a, b, c) = LCM(LCM(a, b), c)

return lcm(lcm(a, b), c);

int main() {

int num1, num2, num3;

// Input three numbers

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

32
// Calculate the LCM of three numbers

int result = lcmThreeNumbers(num1, num2, num3);

// Display the result

cout << "LCM of " << num1 << ", " << num2 << ", and " << num3 << " is " << result << endl;

return 0;

Q – c++ program to find gcd of 3 number using user input

Ans

#include <iostream>

using namespace std;

// Function to find the GCD of two numbers

int gcd(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

// Function to find the GCD of three numbers

int gcdThreeNumbers(int a, int b, int c) {

// GCD(a, b, c) = GCD(GCD(a, b), c)

return gcd(gcd(a, b), c);

int main() {

int num1, num2, num3;

// Input three numbers

cout << "Enter three numbers: ";

33
cin >> num1 >> num2 >> num3;

// Calculate the GCD of three numbers

int result = gcdThreeNumbers(num1, num2, num3);

// Display the result

cout << "GCD of " << num1 << ", " << num2 << ", and " << num3 << " is " << result << endl;

return 0;

Q - c ++ program to reverse a number using user input

Ans

#include <iostream>

using namespace std;

int main() {

int num, reversedNumber = 0, remainder;

// Input the number

cout << "Enter a number: ";

cin >> num;

// Reverse the number

while (num != 0) {

remainder = num % 10;

reversedNumber = reversedNumber * 10 + remainder;

num /= 10;

// Display the reversed number

cout << "Reversed number: " << reversedNumber << endl;

return 0;

34
Q - c++ program to calculate the power of a number using user input

Ans

#include <iostream>

#include <cmath>

using namespace std;

int main() {

double base, exponent, result;

// Input the base and exponent

cout << "Enter the base: ";

cin >> base;

cout << "Enter the exponent: ";

cin >> exponent;

// Calculate the power

result = pow(base, exponent);

// Display the result

cout << base << " raised to the power of " << exponent << " is " << result << endl;

return 0;

35

You might also like