0% found this document useful (0 votes)
9 views10 pages

Programming Fundamentals CP Presentation

Uploaded by

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

Programming Fundamentals CP Presentation

Uploaded by

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

Programming

Fundamentals

Presented by:
Nayyab Afzal (23-PHY-11)
Saarina Irfan (23-PHY-4)
OUTLINE
Problem
Algorithm
Code
Output
Code explanation
Problem:
(4.15)

(Sales-Commission Calculator) A large company pays its


salespeople on a commission basis. The salespeople each receive
$200 per week plus 9% of their gross sales for that week. For
example, a salesperson who sells $5000 worth of chemicals in a
week receives $200 plus 9% of $5000, or a total of $650. Develop a
C++ program that uses a while statement to input each
salesperson’s gross sales for last week and calculates and
displays that salesperson’s earnings. Process one salesperson’s
figures at a time.
Algorithm
1. Prompt for Sales Input:
 Ask the user to enter the gross sales for a salesperson or -1 to
exit.
2. Read the Sales Input:
 Capture the user's input, which is expected to be a floating-point
number.
3. Loop to Calculate Earnings:
 While the sales input is not -1, continue processing.
 Calculate the earnings for the given sales amount based on a fixed
base salary and a commission percentage.
 Display the calculated earnings with a precision of two decimal
places.
 Prompt the user for the next sales input.
4. Exit the Loop:
 If the input is -1, exit the loop and terminate the program.
Code:
#include <iostream> // Include C++ I/O stream library
#include <iomanip> // Include for setting output format
int main() {
// Output message prompting the user to enter sales or -1 to exit
std::cout << " [+] Enter sales in $(or -1 to exit): ";
double sales; // Declare a variable to store sales input
// Read user input from console
std::cin >> sales;
// Loop while the sales input is not -1.0
while (sales != -1.0) {
// Calculate the earnings with a base salary of $200 and 9%
commission
double earning = 200 + ((9.0 / 100.0) * sales);
// Output the earnings with two decimal places
std::cout << "[+] The earning is: $" << std::fixed <<
std::setprecision(2) << earning << "\n";
// Prompt again for the next sales input
std::cout << " [+] Enter sales in $(or -1 to exit): ";
// Read the new sales input
std::cin >> sales;
}
return 0; // Return 0 indicating successful execution
}
OUTPUT
Code Explanation
#include <iostream>
#include <iomanip>
int main() {

The first lines include the necessary libraries for input/output operations and output formatting.
The main function is the starting point of the C++ program.

std::cout << " [+] Enter sales in $(or -1 to exit): ";


This line prints a prompt to the console asking for sales input or a termination condition (-1 to
exit).

double sales;
std::cin >> sales;
A variable sales is declared to store the input sales amount. The std::cin object reads from the
console, allowing the user to enter data.
while (sales != -1.0) {
This while loop keeps running as long as the sales variable is not -1.0. This
structure allows the program to process one salesperson's figures at a time.

double earning = 200 + ((9.0 / 100.0) * sales);


the program calculates the earnings based on the base salary of $200 plus a 9%
commission on the sales amount. The 9.0 / 100.0 represents the commission
rate.

std::cout << "[+] The earning is: $" << std::fixed << std::setprecision(2) <<
earning << "\n";
This line outputs the calculated earnings with two decimal places, using
std::fixed to fix the format and std::setprecision(2) to ensure two digits after the
decimal point.

std::cout << " [+] Enter sales in $(or -1 to exit): ";


After calculating and displaying the earnings, the program prompts the user to
enter another sales amount or -1 to exit the loop.
std::cin >> sales;
The program reads the next sales input, which will determine
whether to continue or exit the loop.

return 0;
}
Once the user enters -1, the loop exits, and the program
returns 0, indicating successful execution.

In conclusion, This C++ program calculates a salesperson's earnings


based on their weekly sales, using a base salary and a commission
rate. It repeatedly prompts for sales input, calculates the earnings,
and outputs the result until the user enters -1 to exit.
THE END

You might also like