0% found this document useful (0 votes)
24 views11 pages

PF-Assignment - 2 - Humna Nehal Butt

The document contains 4 programming questions and their solutions. Question 1 creates a bank account program with options to check balance, deposit, and withdraw. Question 2 implements a number guessing game using a do-while loop for 5 attempts. Question 3 creates a calculator program with 15 mathematical operations. Question 4 explains the use of setw() function to set the width of an output field in formatted output.

Uploaded by

Zarghaam Butt
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)
24 views11 pages

PF-Assignment - 2 - Humna Nehal Butt

The document contains 4 programming questions and their solutions. Question 1 creates a bank account program with options to check balance, deposit, and withdraw. Question 2 implements a number guessing game using a do-while loop for 5 attempts. Question 3 creates a calculator program with 15 mathematical operations. Question 4 explains the use of setw() function to set the width of an output field in formatted output.

Uploaded by

Zarghaam Butt
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/ 11

PF- Assignment No.

2
Decision Making and Loops

Q#1: Write a program that provides the functionality of a bank account. It provides the user
with three options
a. Check the balance (Total Balance)
b. Deposit the balance (The amount is added to current balance)
c. Withdraw the balance (The amount is deducted from the balance)
. The program keeps on running until user ends the program.

#include <iostream>

using namespace std;

class BankAccount {
private:
double balance;

public:
BankAccount() {
balance = 0.0;
}

void checkBalance() {
cout << "Your balance is: $" << balance << endl;
}

void deposit() {
double amount;
cout << "Enter the amount to deposit: $";
cin >> amount;
if (amount > 0) {
balance += amount;
cout << "Deposited $" << amount << " successfully." << endl;
} else {
cout << "Invalid amount. Please enter a positive value." << endl;
}
}

void withdraw() {
double amount;
cout << "Enter the amount to withdraw: $";
cin >> amount;
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrawn $" << amount << " successfully." << endl;
} else if (amount <= 0) {
cout << "Invalid amount. Please enter a positive value." << endl;
} else {
cout << "Insufficient balance for withdrawal." << endl;
}
}
};

int main() {
BankAccount account;
char choice;

do {
cout << "Bank Account Options:" << endl;
cout << "a. Check Balance" << endl;
cout << "b. Deposit" << endl;
cout << "c. Withdraw" << endl;
cout << "q. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 'a':
account.checkBalance();
break;
case 'b':
account.deposit();
break;
case 'c':
account.withdraw();
break;
case 'q':
cout << "Thank you for using our banking system. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 'q');

return 0;
}

Q#2: Using the concept of Do-While loop, write a program that selects a random number and
provides the user 5 attempts to guess the number. It also provides the feedback whether the
number is greater or less than the actual number after every attempt. The program ends
when the number is guessed correctly or the attempts are taken.

#include <iostream>
#include <ctime>
#include <cstdlib>
int main() {
srand(time(0));
int randomNumber = rand() % 100 + 1;
int guess, attempts = 0;

std::cout << "Welcome to the Number Guessing Game!\n";


std::cout << "I have selected a random number between 1 and 100.\n";
std::cout << "You have 5 attempts to guess the number. Good luck!\n";

do {
std::cout << "Enter your guess: ";
std::cin >> guess;

attempts++;

if (guess == randomNumber) {
std::cout << "Congratulations! You have guessed the correct number!\n";
} else if (guess < randomNumber) {
std::cout << "Oops! Your guess is too low. Try again.\n";
} else {
std::cout << "Oops! Your guess is too high. Try again.\n";
}
} while (guess != randomNumber && attempts < 5);

if (attempts == 5) {
std::cout << "Oh no! You have run out of attempts. The correct number was "
<< randomNumber << ".\n";
}

return 0;
}
Q#3: Write a program that creates a calculator which can perform 15
mathematical operations of your own choice. (You may use any built-in
functions/libraries)

#include <iostream>
#include <cmath>

int main() {
int choice;
double num1, num2;

while (true) {
std::cout << "Select an operation:" << std::endl;
std::cout << "1. Addition" << std::endl;
std::cout << "2. Subtraction" << std::endl;
std::cout << "3. Multiplication" << std::endl;
std::cout << "4. Division" << std::endl;
std::cout << "5. Square root" << std::endl;
std::cout << "6. Exponentiation" << std::endl;
std::cout << "7. Logarithm (base 10)" << std::endl;
std::cout << "8. Sine" << std::endl;
std::cout << "9. Cosine" << std::endl;
std::cout << "10. Tangent" << std::endl;
std::cout << "11. Absolute Value" << std::endl;
std::cout << "12. Ceiling" << std::endl;
std::cout << "13. Floor" << std::endl;
std::cout << "14. Minimum" << std::endl;
std::cout << "15. Maximum" << std::endl;
std::cout << "0. Exit" << std::endl;

std::cin >> choice;

if (choice == 0) {
std::cout << "Exiting the calculator program." << std::endl;
break;
}

switch (choice) {
case 1:
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << num1 + num2 << std::endl;
break;
case 2:
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << num1 - num2 << std::endl;
break;
case 3:
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << num1 * num2 << std::endl;
break;
case 4:
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
if (num2 != 0) {
std::cout << "Result: " << num1 / num2 << std::endl;
} else {
std::cout << "Error: Division by zero" << std::endl;
}
break;
case 5:
std::cout << "Enter a number: ";
std::cin >> num1;
if (num1 >= 0) {
std::cout << "Result: " << sqrt(num1) << std::endl;
} else {
std::cout << "Error: Cannot compute square root of a negative number." << std::endl;
}
break;
case 6:
std::cout << "Enter a base number and an exponent: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << pow(num1, num2) << std::endl;
break;
case 7:
std::cout << "Enter a number: ";
std::cin >> num1;
if (num1 > 0) {
std::cout << "Result: " << log10(num1) << std::endl;
} else {
std::cout << "Error: Invalid input for logarithm." << std::endl;
}
break;
case 8:
std::cout << "Enter an angle in degrees: ";
std::cin >> num1;
std::cout << "Result: " << sin(num1 * M_PI / 180.0) << std::endl;
break;
case 9:
std::cout << "Enter an angle in degrees: ";
std::cin >> num1;
std::cout << "Result: " << cos(num1 * M_PI / 180.0) << std::endl;
break;
case 10:
std::cout << "Enter an angle in degrees: ";
std::cin >> num1;
if (fmod(num1, 90.0) != 0) {
std::cout << "Result: " << tan(num1 * M_PI / 180.0) << std::endl;
} else {
std::cout << "Error: Tangent is undefined for multiples of 90 degrees." << std::endl;
}
break;
case 11:
std::cout << "Enter a number: ";
std::cin >> num1;
std::cout << "Result: " << std::abs(num1) << std::endl;
break;
case 12:
std::cout << "Enter a number: ";
std::cin >> num1;
std::cout << "Result: " << std::ceil(num1) << std::endl;
break;
case 13:
std::cout << "Enter a number: ";
std::cin >> num1;
std::cout << "Result: " << std::floor(num1) << std::endl;
break;
case 14:
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << std::min(num1, num2) << std::endl;
break;
case 15:
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << std::max(num1, num2) << std::endl;
break;
default:
std::cout << "Invalid choice. Please select a valid operation." << std::endl;
}
}

return 0;
}

Q#4: What is use the of setw() function. Explain with the help of examples

The setw() function is a part of the iomanip library in C++. It is used to set the width of a field
for the formatted output of values.

This function takes a single argument, which is an integer representing the width of the field.
When the formatted output of a value exceeds this width, it will be truncated and the excess
part will not be displayed.

Example 1: Without using setw()

#include <iostream>
#include <iomanip>
int main() {
int a = 5, b = 50, c = 500;

std::cout << a << std::endl;


std::cout << b << std::endl;
std::cout << c << std::endl;

return 0;
}

In this example, the output will be:

5
50
500

Example 2: Using setw()

#include <iostream>
#include <iomanip>

int main() {
int a = 5, b = 50, c = 500;

std::cout << std::setw(5) << a << std::endl;


std::cout << std::setw(5) << b << std::endl;
std::cout << std::setw(5) << c << std::endl;

return 0;
}

In this example, the setw() function is used to set the width of the field for each variable. The
width is set to 5 characters.
The output of this example will be
5
50
500

As you can see, the output of each variable is displayed within a field of width 5, and any excess
part is truncated.
In summary, the setw() function in C++ is used to set the width of a field for formatted output of
values. It helps to ensure consistent formatting of

You might also like