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

C++ Experiment 2&3

The document describes two programming experiments: the first is a 'guess-the-number' game where a random number between 1 and 1000 is generated, and players must guess it with feedback on their guesses. The second experiment involves creating a banking system with two types of accounts, savings and current, implementing functionalities such as deposits, withdrawals, interest computation, and minimum balance checks using object-oriented programming principles. The provided code snippets illustrate the implementation of both experiments in C++.

Uploaded by

aronadle79
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)
2 views

C++ Experiment 2&3

The document describes two programming experiments: the first is a 'guess-the-number' game where a random number between 1 and 1000 is generated, and players must guess it with feedback on their guesses. The second experiment involves creating a banking system with two types of accounts, savings and current, implementing functionalities such as deposits, withdrawals, interest computation, and minimum balance checks using object-oriented programming principles. The provided code snippets illustrate the implementation of both experiments in C++.

Uploaded by

aronadle79
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/ 6

ARSH MEVATI 23MIP10040

27/03/202

Experiment 2.

Guess-the-number-game: Write a program that pays the game of “guess the number” as
follows: Your program choose the number to be guess by selecting an integer at random
in the range 1 to 1000. The program then displays the following:
I have a number between 1 and 1000. Can you guess my number?
Please type your first guess.
The player then type a first guess. The program responds with one of the following:
1. Excellent ! you guessed the number! Would like to play again (y or n)?
2. Too low. Try again.
3. Too high. Try again.
If the payer’s guess is incorrect, your program should loop until the player finally get the
number right. Your program should keep telling the player Too high or Too low to help
the player “zero in” on the correct answer.

Ans. #include <iostream>


#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
srand(time(0)); // Seed for random number generation
int numberToGuess = rand() % 1000 + 1; // Generating random number between 1 and 1000
int guess;
char playAgain = 'y';

while (playAgain == 'y') {


cout << "I have a number between 1 and 1000. Can you guess my number?\n";
cout << "Please type your first guess: ";

do {
cin >> guess;
if (guess == numberToGuess) {
cout << "Excellent! You guessed the number! Would you like to play again (y or n)? ";
cin >> playAgain;
if (playAgain == 'y') {
numberToGuess = rand() % 1000 + 1;
cout << "I have a number between 1 and 1000. Can you guess my number?\n";
cout << "Please type your first guess: ";
}
break;
} else if (guess < numberToGuess) {
cout << "Too low. Try again: ";
} else {
cout << "Too high. Try again: ";
}
} while (guess != numberToGuess);
}

return 0;
}

Experiment 3

Assume that a bank maintains two kinds of account for its customers, one called saving
account and the other current account. The saving 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 falls below this level, a service charge is imposed.
Create a class Account that stores customer name, account number, and
l

type of account. From this device the classes Curr-acct and Sav-acct to make them
more specific to their requirements. Include the necessary methods in order to achieve
the following tasks.

Do not
Accept deposit from a customer and update the balance Display the balance
Compute and deposit interest
Permit withdrawal and update the balance
Check for the minimum balance, impose penalty, if necessary and update the balance.
use any constructors. Use methods to initialize the class members.

Ans. #include <iostream>


#include <string>

using namespace std;

class Account {
protected:
string customerName;
string accountNumber;
string accountType;
float balance;

public:
void setCustomerName(string name) {
customerName = name;
}

void setAccountNumber(string number) {


accountNumber = number;
}

void setAccountType(string type) {


accountType = type;
}

void displayBalance() {
cout << "Balance: " << balance << endl;
}

virtual void deposit(float amount) = 0;

virtual void withdraw(float amount) = 0;

virtual void computeInterest() = 0;

virtual void checkMinimumBalance() = 0;


};

class Curr_acct : public Account {


float minimumBalance;
float penaltyCharge;

public:
void setMinimumBalance(float minBal) {
minimumBalance = minBal;
}

void setPenaltyCharge(float penalty) {


penaltyCharge = penalty;
}
void deposit(float amount) override {
balance += amount;
cout << "Deposit of " << amount << " successful." << endl;
}

void withdraw(float amount) override {


if (balance - amount < minimumBalance) {
cout << "Insufficient balance. Penalty charge will be imposed." << endl;
balance -= penaltyCharge;
} else {
balance -= amount;
cout << "Withdrawal of " << amount << " successful." << endl;
}
}

void computeInterest() override {


cout << "Current account does not earn interest." << endl;
}

void checkMinimumBalance() override {


if (balance < minimumBalance) {
cout << "Minimum balance not maintained. Penalty charge will be imposed." << endl;
balance -= penaltyCharge;
}
}
};

class Sav_acct : public Account {


float interestRate;

public:
void setInterestRate(float rate) {
interestRate = rate;
}

void deposit(float amount) override {


balance += amount;
cout << "Deposit of " << amount << " successful." << endl;
}

void withdraw(float amount) override {


if (balance - amount < 0) {
cout << "Insufficient balance." << endl;
} else {
balance -= amount;
cout << "Withdrawal of " << amount << " successful." << endl;
}
}

void computeInterest() override {


balance += balance * (interestRate / 100);
cout << "Interest computed and added to the balance." << endl;
}

void checkMinimumBalance() override {


// No minimum balance check for saving account
}
};

int main() {
Sav_acct savingsAccount;
Curr_acct currentAccount;

savingsAccount.setCustomerName("John Doe");
savingsAccount.setAccountNumber("SAV123");
savingsAccount.setAccountType("Savings");
savingsAccount.setInterestRate(5.0); // Example interest rate

currentAccount.setCustomerName("Jane Smith");
currentAccount.setAccountNumber("CURR456");
currentAccount.setAccountType("Current");
currentAccount.setMinimumBalance(1000); // Example minimum balance
currentAccount.setPenaltyCharge(50); // Example penalty charge

savingsAccount.deposit(1000);
savingsAccount.computeInterest();
savingsAccount.displayBalance();

currentAccount.deposit(2000);
currentAccount.checkMinimumBalance();
currentAccount.displayBalance();

currentAccount.withdraw(1500);
currentAccount.checkMinimumBalance();
currentAccount.displayBalance();

return 0;
}

You might also like