0% found this document useful (0 votes)
24 views

C++ File

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C++ File

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Q1.

CODE:

#include <iostream>

#include <string>

using namespace std;

class BankAccount {

private:

string depositorName;

string accountNumber;

string accountType;

double balance;

public:

// Constructor to initialize account details

BankAccount(string name, string accNumber, string accType, double initialBalance)

: depositorName(name), accountNumber(accNumber), accountType(accType),


balance(initialBalance) {}

// Method to deposit an amount into the account

void deposit(double amount) {

if (amount > 0) {

balance += amount;

cout << "Deposited: $" << amount << endl;

} else {

cout << "Deposit amount must be positive." << endl;

// Method to withdraw an amount from the account after checking balance

void withdraw(double amount) {

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

balance -= amount;

cout << "Withdrew: $" << amount << endl;

} else if (amount > balance) {


cout << "Insufficient funds." << endl;

} else {

cout << "Withdrawal amount must be positive." << endl;

// Method to display the name and balance of the account

void display() const {

cout << "Depositor Name: " << depositorName << endl;

cout << "Account Number: " << accountNumber << endl;

cout << "Account Type: " << accountType << endl;

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

};

int main() {

// Create a BankAccount object with initial values

BankAccount myAccount("John Doe", "123456789", "Savings", 1000.00);

// Display account details

myAccount.display();

// Deposit some money

myAccount.deposit(500.00);

// Withdraw some money

myAccount.withdraw(200.00);

// Attempt to withdraw more than the balance

myAccount.withdraw(2000.00);

// Display updated account details

myAccount.display();

return 0;

OUTPUT:
Q2.

CODE:

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

void playGame() {

// Generate a random number between 1 and 1000

int numberToGuess = rand() % 1000 + 1;

int userGuess = 0;

bool guessedCorrectly = false;

cout << "I have a number between 1 and 1000." << endl;

cout << "Can you guess my number?" << endl;

while (!guessedCorrectly) {

cout << "Please type your guess: ";

cin >> userGuess;

if (userGuess > numberToGuess) {

cout << "Too high. Try again." << endl;

} else if (userGuess < numberToGuess) {

cout << "Too low. Try again." << endl;


} else {

cout << "Excellent! You guessed the number!" << endl;

guessedCorrectly = true;

int main() {

// Seed the random number generator

srand(static_cast<unsigned int>(time(0)));

char playAgain = 'y';

while (playAgain == 'y' || playAgain == 'Y') {

playGame();

cout << "Would you like to play again (y or n)? ";

cin >> playAgain;

cout << "Thank you for playing!" << endl;

return 0;

OUTPUT:
Q3.

CODE:

#include <iostream>

#include <string>

using namespace std;

class Account {

protected:

string customerName;

string accountNumber;

string accountType;

double balance;

public:

// Method to initialize account details

void initialize(string name, string accNumber, string accType, double initialBalance) {

customerName = name;

accountNumber = accNumber;

accountType = accType;

balance = initialBalance;

// Method to deposit an amount

void deposit(double amount) {

if (amount > 0) {

balance += amount;

cout << "Deposited: $" << amount << endl;

} else {

cout << "Deposit amount must be positive." << endl;

// Method to display the balance

void displayBalance() const {


cout << "Account Number: " << accountNumber << endl;

cout << "Account Type: " << accountType << endl;

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

// Method to withdraw an amount

virtual void withdraw(double amount) {

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

balance -= amount;

cout << "Withdrew: $" << amount << endl;

} else if (amount > balance) {

cout << "Insufficient funds." << endl;

} else {

cout << "Withdrawal amount must be positive." << endl;

// Method to compute interest, specific to Savings Account

virtual void computeAndDepositInterest() {

// Default implementation (no interest for base class)

cout << "Interest computation not applicable for this account type." << endl;

// Method to check minimum balance, specific to Current Account

virtual void checkMinimumBalance() {

// Default implementation (no minimum balance for base class)

cout << "Minimum balance check not applicable for this account type." << endl;

};

// Class for Current Account

class CurrAcct : public Account {

private:

double minBalance;

double serviceCharge;
public:

// Initialize current account with minimum balance and service charge

void initialize(string name, string accNumber, double initialBalance, double minBal, double charge)
{

Account::initialize(name, accNumber, "Current", initialBalance);

minBalance = minBal;

serviceCharge = charge;

// Withdraw with minimum balance check

void withdraw(double amount) override {

if (amount > 0 && (balance - amount) >= minBalance) {

balance -= amount;

cout << "Withdrew: $" << amount << endl;

} else if (amount > balance) {

cout << "Insufficient funds." << endl;

} else {

cout << "Withdrawal would result in balance below minimum required." << endl;

// Check and impose service charge if below minimum balance

void checkMinimumBalance() override {

if (balance < minBalance) {

balance -= serviceCharge;

cout << "Minimum balance not maintained. Service charge imposed: $" << serviceCharge <<
endl;

};

// Class for Savings Account

class SavAcct : public Account {

private:

double interestRate;
public:

// Initialize savings account with interest rate

void initialize(string name, string accNumber, double initialBalance, double rate) {

Account::initialize(name, accNumber, "Savings", initialBalance);

interestRate = rate;

// Compute and deposit interest

void computeAndDepositInterest() override {

double interest = balance * interestRate / 100;

balance += interest;

cout << "Interest computed and added: $" << interest << endl;

};

int main() {

// Create and initialize Current Account

CurrAcct currentAccount;

currentAccount.initialize("Alice Smith", "CA123456", 500.00, 100.00, 5.00);

// Create and initialize Savings Account

SavAcct savingsAccount;

savingsAccount.initialize("Bob Johnson", "SA987654", 1000.00, 2.5);

// Display initial balances

cout << "Current Account Details:" << endl;

currentAccount.displayBalance();

cout << "\nSavings Account Details:" << endl;

savingsAccount.displayBalance();

// Perform transactions

cout << "\nPerforming transactions on Current Account:" << endl;

currentAccount.deposit(200.00);

currentAccount.withdraw(150.00);

currentAccount.checkMinimumBalance(); // Check and impose service charge if necessary

currentAccount.displayBalance();
cout << "\nPerforming transactions on Savings Account:" << endl;

savingsAccount.deposit(300.00);

savingsAccount.withdraw(100.00); // Withdraw is allowed in Savings Account (default


implementation)

savingsAccount.computeAndDepositInterest(); // Compute and deposit interest

savingsAccount.displayBalance();

return 0;

OUTPUT:

Q4.

CODE:

#include <iostream>

#include <vector>

using namespace std;

int main() {

const int NUM_CANDIDATES = 5; // Number of candidates


vector<int> voteCounts(NUM_CANDIDATES, 0); // Vector to store vote counts for each candidate

int spoiltBallots = 0; // Counter for spoilt ballots

cout << "Enter votes for the candidates (1 to 5). Enter 0 to end voting." << endl;

while (true) {

int vote;

cout << "Enter vote: ";

cin >> vote;

if (vote == 0) {

// End voting process

break;

} else if (vote >= 1 && vote <= NUM_CANDIDATES) {

// Valid vote for a candidate

voteCounts[vote - 1]++;

} else {

// Invalid vote (spoilt ballot)

spoiltBallots++;

// Display the results

cout << "\nElection Results:" << endl;

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

cout << "Candidate " << (i + 1) << ": " << voteCounts[i] << " votes" << endl;

cout << "Spoilt ballots: " << spoiltBallots << endl;

return 0;

OUTPUT:

You might also like