Example 1-1 (cond't.
)
⚫Algorithm:
Get length of the rectangle
Get width of the rectangle
Find the perimeter using the following equation:
perimeter = 2 * (length + width)
Find the area using the following equation: area length width
program:
#include <iostream>
using namespace std;
int main() {
// Declare variables for length and width
double length, width;
// Get length and width from user
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
// Calculate the perimeter and area
double perimeter = 2 * (length + width);
double area = length * width;
// Display the results
cout << "Perimeter of the rectangle: " << perimeter << endl;
cout << "Area of the rectangle: " << area << endl;
return 0;
Example 1-3
⚫Every salesperson has a base salary ⚫Salesperson receives $10 bonus at the end of the month
for each year worked if he or she has been with the store for five or less years
⚫The bonus is $20 for each year that he or she has worked there if over 5 years
program:
#include <iostream>
using namespace std;
int main() {
// Declare variables for base salary and years worked
double baseSalary;
int yearsWorked;
double bonus = 0;
// Get the base salary from user
cout << "Enter the base salary of the salesperson: $";
cin >> baseSalary;
// Get the number of years worked from user
cout << "Enter the number of years the salesperson has worked: ";
cin >> yearsWorked;
// Calculate bonus based on years worked
if (yearsWorked <= 5) {
bonus = yearsWorked * 10; // $10 for each year worked (<= 5 years)
} else {
bonus = yearsWorked * 20; // $20 for each year worked (> 5 years)
// Calculate total salary including bonus
double totalSalary = baseSalary + bonus;
// Display the total salary and bonus
cout << "Bonus for " << yearsWorked << " years of work: $" << bonus << endl;
cout << "Total salary including bonus: $" << totalSalary << endl;
return 0;
Example 1-3 (cond't.)
Additional bonuses are as follows:
- If total sales for the month are $5,000- $10,000, he or she receives a 3% commission on the
sale
If total sales for the month are at least $10,000, he or she receives a 6% commission on the sale
program:
#include <iostream>
using namespace std;
int main() {
// Declare variables for total sales and commission
double totalSales;
double commission = 0;
// Get the total sales from the user
cout << "Enter the total sales for the month: $";
cin >> totalSales;
// Check the sales range and calculate commission
if (totalSales >= 5000 && totalSales < 10000) {
commission = totalSales * 0.03; // 3% commission if sales are between $5,000 and
$10,000
else if (totalSales >= 10000) {
commission = totalSales * 0.06; // 6% commission if sales are $10,000 or more
// Print the commission
cout << "Commission: $" << commission << endl;
return 0;