Assignment 2 PF - 1144 Usman Abbas - Spring 2024 - Copy (AutoRecovered)
Assignment 2 PF - 1144 Usman Abbas - Spring 2024 - Copy (AutoRecovered)
Submitted by
Usman Abbas
1144-FOC/BSIT/F23
Sec-B
Submitted to
Q.2
//Write a program that prompts the user to input a decimal number and
outputs the number rounded to the nearest integer.
#include <iostream>
using namespace std;
int main()
{
double decimalNumber;
int roundedNumber;
cout << "Enter a decimal number: ";
cin >> decimalNumber;
if (decimalNumber >= 0)
roundedNumber = (int)(decimalNumber + 0.5);
else
roundedNumber = (int)(decimalNumber - 0.5);
cout<<"Rounded to the nearest integer: "<<roundedNumber<< endl;
system("pause"); return 0; }
Page 2(6)
Q.3
// Write a program that prompts the capacity, in ....... without
refueling.
#include <iostream>
using namespace std;
int main() {
double tankCapacity, milesPerGallon, milesWithoutRefueling;
cout<<"Enter the capacity of the fuel tank (in gallons): ";
cin>>tankCapacity;
cout<<"Enter the miles per gallon the automobile can be driven:
";
cin>>milesPerGallon;
milesWithoutRefueling = tankCapacity * milesPerGallon;
cout<<"The automobile can be driven for
"<<milesWithoutRefueling<<" miles without refueling."<<endl;
system("pause"); return 0; }
Q.4
// Write a C++ program that prompts the user to input ..... output is
2:40:30.)
#include <iostream>
using namespace std;
int main() {
int elapsed_time, hours, minutes, seconds;
cout << "Enter the elapsed time for an event in seconds: ";
cin >> elapsed_time;
hours = elapsed_time / 3600;
elapsed_time %= 3600;
minutes = elapsed_time / 60;
seconds = elapsed_time % 60;
cout << "Elapsed time: " << hours << ":" << minutes << ":" <<
seconds <<endl;
system("pause");
return 0; }
Q.5
// To make a profit, a local store marks up the ...... selling price
plus the sales tax.)
#include <iostream>
using namespace std;
int main() {
Page 3(6)
double original_price, markup_percentage, sales_tax_rate,
selling_price,sales_tax ,final_price ;
cout<<"Enter the original price of the item: ";
cin>>original_price;
cout<<"Enter the percentage of the mark-up price: ";
cin>>markup_percentage;
cout<<"Enter the sales tax rate: ";
cin>>sales_tax_rate;
selling_price=original_price+(original_price*markup_percentage/10
0.0);
sales_tax = selling_price * sales_tax_rate / 100.0;
final_price = selling_price + sales_tax;
cout<<"\nOriginal price of the item: $"<<original_price<<endl;
cout<<"Percentage of the mark-up: "<<markup_percentage
<<"%"<<endl;
cout<<"Store's selling price of the item:
$"<<selling_price<<endl;
cout<<"Sales tax rate: " <<sales_tax_rate<<"%"<<endl;
cout<<"Sales tax: $" <<sales_tax<<endl;
cout<<"Final price of the item: $"<<final_price<<endl;
system("pause"); return 0; }
Q.6
// A milk carton can hold 3.78 liters of milk......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 milk cartons needed to hold milk. (Round
your answer to the nearest integer.)
// c. Outputs the cost of producing milk.
// d. Outputs the profit for producing milk.
#include <iostream>
#include <cmath>
using namespace std;
const double CARTON_CAPACITY = 3.78;
const double COST_PER_LITER = 0.38;
const double PROFIT_PER_CARTON = 0.27;
int main() {
double total_milk_produced, cost_of_production, profit ;
int num_cartons;
Page 4(6)
cout << "Enter the total amount of milk produced in the
morning (in liters): ";
cin >> total_milk_produced;
num_cartons = (int)(ceil(total_milk_produced /
CARTON_CAPACITY));
cost_of_production = total_milk_produced * COST_PER_LITER;
profit = num_cartons * PROFIT_PER_CARTON;
cout<<"\nNumber of milk cartons needed: "<<num_cartons<<endl;
cout<<"Cost of producing milk: $"<<cost_of_production<<endl;
cout<<"Profit for producing milk: $"<<profit<<endl;
system("pause"); return 0;}
Q.7
// You found an exciting summer job for five weeks...... program then
outputs the following:
// a. Your income before and after taxes from your summer job.
// b. The money you spend on clothes and other accessories.
// c. The money you spend on school supplies.
// d. The money you spend to buy savings bonds.
// e. The money your parents spend to buy additional savings bonds for
you.
#include <iostream>
#include <iomanip>
using namespace std;
const double TAX_RATE = 0.14;
const double CLOTHES_PERCENTAGE = 0.10;
const double SUPPLIES_PERCENTAGE = 0.01;
const double SAVINGS_BONDS_PERCENTAGE = 0.25;
const double PARENTS_MATCH_RATIO = 0.50;
int main() {
double payRatePerHour;
int hoursWorkedPerWeek;
cout << "Enter the pay rate per hour: $";
cin >> payRatePerHour;
cout << "Enter the number of hours worked per week: ";
cin >> hoursWorkedPerWeek;
double grossIncome = payRatePerHour * hoursWorkedPerWeek * 5;
double netIncome = grossIncome * (1 - TAX_RATE);
double clothesExpense = netIncome * CLOTHES_PERCENTAGE;
double suppliesExpense = netIncome * SUPPLIES_PERCENTAGE;
double savingsBondsExpense = netIncome *
SAVINGS_BONDS_PERCENTAGE;
double parentsSavingsBonds = savingsBondsExpense *
PARENTS_MATCH_RATIO;
cout<<fixed<<setprecision(2);
Page 5(6)
cout<<"a. Gross income before taxes: $"<<grossIncome<<endl;
cout << " Net income after taxes: $" <<netIncome<<endl;
cout<<"b. Money spent on clothes and accessories:
$"<<clothesExpense<<endl;
cout << "c. Money spent on school supplies:
$"<<suppliesExpense<<endl;
cout << "d. Money spent on savings bonds: $" <<
savingsBondsExpense << endl;
cout << "e. Parents' contribution to additional savings bonds:
$" << parentsSavingsBonds <<endl;
system("pause"); return 0;}
Q.8
// A contractor orders, say, 30 cubic yards ...... square root of
a decimal number. For example, sqrt(16.0) = 4.0.)
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double concreteOrdered, thickness, ratio;
cout<<"Enter the amount of premixed concrete ordered (in cubic
yards): ";
cin >> concreteOrdered;
cout << "Enter the thickness of the patio (in inches): ";
cin >> thickness;
cout << "Enter the ratio of length to width: ";
cin >> ratio;
double thicknessFeet = thickness / 12.0;
double concreteCubicFeet = concreteOrdered * 27.0;
double area = concreteCubicFeet / thicknessFeet;
double width = sqrt(area / (1 + ratio));
double length = ratio * width;
cout << "Length of the patio: " << length << " feet" <<endl;
cout << "Width of the patio: " << width << " feet" <<endl;
system("pause"); return 0;}
Page 6(6)