C++ Experiment 2&3
C++ Experiment 2&3
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.
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';
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.
class Account {
protected:
string customerName;
string accountNumber;
string accountType;
float balance;
public:
void setCustomerName(string name) {
customerName = name;
}
void displayBalance() {
cout << "Balance: " << balance << endl;
}
public:
void setMinimumBalance(float minBal) {
minimumBalance = minBal;
}
public:
void setInterestRate(float rate) {
interestRate = rate;
}
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;
}