100% found this document useful (1 vote)
291 views

Lab 2 - Intro C++

The document contains 4 C++ programs with explanations: 1. A program that takes a user input number, performs calculations with constants, and displays the mystery number output. 2. A program that calculates mark-up pricing for store items including original price, percentage mark-up, sales tax, and final price. 3. A program that calculates milk production needs including number of cartons, cost to produce, and profit based on user input amount of milk produced. 4. A program that calculates earnings, taxes, expenses, investments, and remaining money from a part-time job based on hourly rate and hours worked input by the user.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
291 views

Lab 2 - Intro C++

The document contains 4 C++ programs with explanations: 1. A program that takes a user input number, performs calculations with constants, and displays the mystery number output. 2. A program that calculates mark-up pricing for store items including original price, percentage mark-up, sales tax, and final price. 3. A program that calculates milk production needs including number of cartons, cost to produce, and profit based on user input amount of milk produced. 4. A program that calculates earnings, taxes, expenses, investments, and remaining money from a part-time job based on hourly rate and hours worked input by the user.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 2

1. Trace and show the output of the following program. Suppose the input is:
340

#include<iostream>

using namespace std;

const int PRIME_NUM = 11; //Constant declaration - global

int main(){

const int SECRET = 17; //Constant declaration - local

int num, mysteryNum;

cout << "Enter a positive integer less than 1000: "<<endl;


cin >> num;

mysteryNum = num *(PRIME_NUM - 3)* SECRET;

cout << "Mystery number: " << mysteryNum << endl;

return 0;
}

2. To make a profit, a local store marks up the prices of its items by a certain percentage.
Write a complete C++ program that reads the original price of the items sold, the
percentage of the mark-up price, and the sales tax rate. The program then outputs the
original price of the item, the percentage of the mark-up, the store’s selling price of the
item, the sales tax rate, the sales tax amount, and the final price of the item. (The final
price of the item is the selling price plus the sales tax.)

#include <iostream>

using namespace std;

int main()
{
double originalPrice, salesTaxRate, totalPrice,
1
markupPercentage, salesTaxPrice, markupPrice;

cout << "Please enter the original price: RM";


cin >> originalPrice;

cout << "Please enter the mark-up percentage: ";


cin >> markupPercentage;

cout << "Please enter the sales tax percentage: ";


cin >> salesTaxRate;

markupPrice = originalPrice * (markupPercentage / 100);


salesTaxPrice = originalPrice * (salesTaxRate / 100);
totalPrice = originalPrice + salesTaxPrice +
markupPrice;

cout << "Original Price: RM" << originalPrice << endl;


cout << "Sales Tax: RM" << salesTaxPrice << endl;
cout << "Mark-Up: RM" << markupPrice << endl;
cout << "Total Price: RM" << totalPrice << endl;

return 0;
}

2
3. A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships cartons of
dairy milk to a local grocery store. The cost of producing one liter of milk is RM 0.38
and the profit of each carton of milk is RM 0.27. Write a complete C++ program that
does the following:

a. Prompts the user to enter the total amount of milk produced in the morning.
b. Outputs the number of milks carton needed to hold milk.
c. Outputs the cost of producing milk.
d. Outputs the profit of producing milk.

#include <iostream>
#include <iomanip>

using namespace std;

int main(){
double milkProduced, cartonsRequired;

const double cartonSize = 3.78;


const double productionCost = 0.38;
const double cartonProfit = 0.27;

cout << "How much milk did you produce? ";


cin >> milkProduced;

cartonsRequired = milkProduced / cartonSize;

cout << fixed << showpoint << setprecision(2);

cout << "That is going to require " <<


static_cast<int>(cartonsRequired) << " cartons" <<
endl;
cout << "Total Cost to Produce: $" << cartonsRequired *
productionCost << endl;
cout << "Total Profit: $" << cartonsRequired * cartonProfit <<
endl;

3
return 0;
}

4. You found an exciting part-time job during the UTP semester break, in which you
worked for a period of 7 weeks. It pays, say, RM 15.50 per hour. Suppose that the total
tax you pay on your income from the job is 14%. After paying the taxes, you spend
25% of your net income to buy new clothes and other accessories for the incoming
new semester and 4% to buy stationeries. After buying clothes and stationeries, you
use 45% of the remaining money to invest in unit trust. For each RM 2.00 that you
spend on unit trust, your parents spend RM 0.50 to buy additional unit trust for you.
Write a complete C++ program that will prompts the user to enter the number of hours
worked for each week, which may be different from one week to the other. The
program then outputs the following:

a. Your income before and after taxes from your part-time job.
b. The money you spend on clothes and other accessories.
c. The money you spend on stationeries.
d. The money that you invest on unit trust.
e. The money your parents spend to buy additional unit trust for you.
f. The money left for you to bring back to UTP.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double payRate, grossIncome, netIncome, stationeryAmount,
trustAmount;
double clothesAmount, parentsBondsAmount, hoursWorked;

const double TAX_RATE = 0.14;


const double CLOTHES_PERCENTAGE_OF_INCOME = 0.25;
const double STATIONERIES_PERCENTAGE_OF_INCOME = 0.04;
const double UNIT_TRUST_PERCENTAGE_OF_INCOME = 0.45;

4
const double PARENTS_CO_CONTRIBUTION_AMOUNT = 0.50;

cout << "How many hours did you work: ";


cin >> hoursWorked;

cout << "What was the hourly rate: RM";


cin >> payRate;

grossIncome = hoursWorked * payRate;


netIncome = grossIncome *= TAX_RATE;
clothesAmount = netIncome * CLOTHES_PERCENTAGE_OF_INCOME;
stationeryAmount = netIncome * STATIONERIES_PERCENTAGE_OF_INCOME;
// Calculate what is now left
netIncome -= (clothesAmount + stationeryAmount);
trustAmount = netIncome * UNIT_TRUST_PERCENTAGE_OF_INCOME;
parentsBondsAmount = trustAmount * PARENTS_CO_CONTRIBUTION_AMOUNT;

cout << fixed << showpoint << setprecision(2);

cout << "Gross Income: RM" << grossIncome << endl;


cout << "Net Income: RM" << netIncome << endl;
cout << "Clothes & Accessories: RM" << clothesAmount << endl;
cout << "Stationeries Supplies: RM" << stationeryAmount << endl;
cout << "Unit Trust Investment: RM" << trustAmount << endl;
cout << "Parents Bonds Co-Contribution: RM" <<parentsBondsAmount
<< endl;

return 0;
}

You might also like