Assignment # 1
Programming Fundamentals
Name: Abdul Rehman Roll Num: 70131431
1. Design an algorithm to convert the change given in quarters, dimes, nickels, and
pennies into pennies. Also write a C++ code and show the output.
#include <iostream>
using namespace std;
int main ( )
{
float userNumber;
int change, quarters, dimes, nickels, pennies;
cout <<"Enter the amount of money: ";
cin >> userNumber;
change = userNumber * 100;
quarters = change / 25;
change = change % 25;
dimes = change / 10;
change = change % 10;
nickels = change / 5;
pennies = change ;
cout << "\nQuarters: " << quarters << endl;
cout << " Dimes: " << dimes << endl;
cout << " Nickels: " << nickels << endl;
cout <<" Pennies: " << pennies << endl;
return 0;
}
Pseudocode:
Declare a float variable named UserNumber.
Declare change,quarter,dimes, nickles and pennies as ints.
Print “Enter the amount of money: ” and then store input in UserNumber.
Assign ‘userNumber * 100’ to change. Assign ‘change / 25’ to quarters. Assign ‘change % 25’ to change.
Assign ‘change / 5’ to nickels. Assign change to pennies.
Print quarters, dimes, nickels, pennies.
Assignment # 1
2. Given the radius, in inches, and price of a pizza, design an
algorithm to find the price of the
pizza per square inch. Also write a C++ code and show the output.
#include <iostream>
using namespace std;
int main() {
int radius, price_of_pizza;
double area_of_pizza,price;
cout<<"Enter the radius of the pizza: ";
cin>>radius;
cout<<"Enter the prize of the pizza: ";
cin>>price_of_pizza;
area_of_pizza=3.14*radius*radius;
price=price_of_pizza/area_of_pizza;
cout<<"Price of the pizza per square inch is equal to: "<<price;
return 0;
Pseudo Code:
Declare radius, price_of_pizza as ints. area_of_pizza and price as doubles.
Print “Enter the radius of the pizza:” and store the input in price_of_pizza.
Calculate the area with ‘3.14*radius*radius’ and store it in area_of_pizza.
Assign price_of_pizza/area_of_pizza to price.
Print price.
Assignment # 1
3. To make a profit, the prices of the items sold in a furniture store are
marked up by 80%.
After marking up the prices each item is put on sale at a discount of
10%. Design an
algorithm to find the selling price of an item sold at the furniture store.
What information do
you need to find the selling price? Also write a C++ code and show the
output.
#include <iostream>
using namespace std;
int main()
{
double a,b,z,s;
cout<<"Enter the original price : "<<
cin>>a;
b=a+(a*0.8);
z=b*0.1;
s=b-z;
cout<<"Original Price : "<<a<<endl;
cout<<"Selling Price : "<<s;
return 0;
}
Declare a,b,z,s as doubles. Get input from use and store it in a.
Assign b = a+(a*0.8). z = b*0.1, s = b – z.
Print a as the original price, and print s as the selling price.
Assignment # 1
5. Design an algorithm and write a C++ program that prompts the user
to input the elapsed time
for an event in seconds. The program then outputs the elapsed time in
hours, minutes, and
seconds.
#include <iostream>
using namespace std;
int main()
{
int secElapsed, hours, min, sec;
int secPerMin = 60;
int secPerHour = 60 * secPerMin;
cout << "Enter the number of seconds elapsed: ";
cin >> secElapsed;
hours = secElapsed / secPerHour;
secElapsed = secElapsed % secPerHour;
min = secElapsed / secPerMin;
sec = secElapsed % secPerMin;
cout << hours << ":" << min << ":" << sec << endl;
return 0;
}
Declare secElapsed, hours, min, sec.
Assign and declare secPerMin = 60, secPerHour = 60 * secPerMin.
Get secElapsed from user.
Assign secElapsed/secPerHour to hours.
Assign secElapsed % secPerHour to secElapsed.
Assign secElapsed / secPerMin to min.
Assign secElapsed % secPerMin.
Display hours:min:sec.
Assignment # 1
Design an algorithm to find the weighted average of four test scores.
The four test scores and
their respective weights are given in the following format: testScore1
weightTestScore1....
Also write a C++ code and show the output.
For example, sample data is as follows:
75 0.20
95 0.35
85 0.15
65 0.30
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
int TestScore[4];
double WeightedScore[4];
int TestScore1 = 75;
int TestScore2 = 95;
int TestScore3 = 85;
int TestScore4 = 65;
double WeightedScore1 = 0.20;
double WeightedScore2 = 0.35;
double WeightedScore3 = 0.15;
double WeightedScore4 = 0.30;
cout<<"The Weighted Average of Test Score is: "<<TestScore1*(WeightedScore1)
+ TestScore2*(WeightedScore2) + TestScore3*(WeightedScore3) +
TestScore4*(WeightedScore4)<<endl;
return 0;
}
Declare all testscores as ints and all the weighted scores as doubles.
Calculate weighted score using the formla and print the result.
Assignment # 1