Professional Documents
Culture Documents
BS-Software Engineering
Section X
Submitted to:
Afia Mufti
Submitted by:
ID: 13005065118
Chapter 2
a) (x + z) % y = 3 b) (x + y) % w = 0.5
c) ( y + w) % x = 4.5 d) (x + y) * w = 38.5
e) ( x % y) % z = 1 f) (y % z) % x = 2
g) (x * z) % y = 2 h) ((x * y) * w) * z = 420
ANS)
int x,y;
x = 25;
y = 18;
cout<<"x = "<<x<<endl;
cout<<"y = "<<y;
b) Declare and initialize an int variable temp to 10 and a char variable ch to 'A'.
ANS)
int temp;
char ch ;
temp = 10;
ch = 'A' ;
cout<<"temp = "<<temp<<endl;
cout<<"ch = "<<ch;
ANS)
int x,a;
cout<<"enter x: ";
cin>>x;
a = x + 5;
cout<<"a = "<<a;
ANS)
double payRate;
payRate = 12.50;
cout<<"payRate = "<<payRate;
e ) Copy the value of an int variable firstNum into an int variable tempNum.
f) Swap the contents of the int variables x and y. (Declare additional variables, if necessary.)
ANS)
int x,y,temp;
cout << "enter x: " ;
cin>>x;
cout<<"enter y: ";
cin>>y;
temp = x;
x = y;
y = temp;
cout<<"x: "<<x<<endl;
cout<<"y: "<<y;
g) Suppose x and y are double variables. Output the contents of x, y, and the expression x + 12 / y -
18.
ANS)
double x,y,a;
cin>>x;
cout<<"enter y: ";
cin>>y;
a = x + 12 / y - 18;
cout<<"x + 12 / y - 18 = "<<a;
h) Declare a char variable grade and set the value of grade to 'A'.
ANS)
char grade;
grade = 'A';
cout<<"grade = "<<grade;
ANS)
int a,b,c,d;
cout<<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
cout<<"c = ";
cin>>c;
cout<<"d = ";
cin>>d;
ANS) double z ;
int x;
z = 12.34 ;
x = z;
cout<<"x= "<<x;
return 0;
a) 32 times a plus b =
int a,b,c;
cout<<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
c = 32 * a + b;
cout<<"c = "<<c;
ANS)
Char a;
cout<<"a=8"<<a;
ANS)
string JulieNelson;
cout<<"JulieNelson"<<JulieNelson;
d) (b2 -4ac)/2a
e) (a + b)/c(ef)-gh
ANS)
int b,a,c,e,g,h,f;
cout<<"(-b + (b^2 (pow(b,2)) - 4*a*c)) / 2*a" <<(-b + (b^2 (pow(b,2)) - 4*a*c)) / 2*a <<endl;
Q15) Suppose x, y, and z are int variables and w and t are double variables.
What value is assigned to each of these variables after the last statement
executes?
ANS)
X=20
y=15
z=6
w=12.2
t=4.5
ANS) fName=
ANS) disPrice=
ANS) noJuicebottles=
ANS) milesTraveled=
ANS) highscore=
Q21) Write C++ statements to do the following.
cout<<"num1= ";
cout<<"num2= ";
c) Input the first number in num1 and the second number in num2.
cout<<"num1= ";
cin>>num1;
cout<<"num2= ";
cin>>num2;
d) Output num1, num2, and 2 times num1 minus num2. Your output must
identify each number and the expression.
ANS)
int num1,num2,a;
cout<<"num1= ";
cin>>num1;
cout<<"num2= ";
cin>>num2;
a=2*num1+num2;
cout<<"a= "<<a;
Q30) Suppose a, b, and sum are int variables and c is a double variable. What
value is assigned to each variable after each statement executes? Suppose a = 3,
b = 5, and c = 14.1.
b += c – a= 16
a *= 2 * b + c= 44
ANS)
int ccccccccc,cc;
cout<<"ccccccccc" <<endl;
cout<<"cc"<<endl;
cout<<"cc"<<endl;
cout<<"cc"<<endl;
cout<<"cc"<<endl;
cout<<"ccccccccc"<<endl;
int cc;
return 0;
b) Write a C++ statement that allows you to use cin, cout, and endl
without the prefix std::
int main()
int c;
cin>>c;
return 0;
c) Write C++ statements that declare the following variables: num1, num2, num3, and average
of type int.
{
int num1,num2,num3,average;
return 0;
}
d) Write C++ statements that store 125 into num1, 28 into num2, and
-25 into num3.
int main()
{
int num1,num2,num3;
num1 = 125;
num2 = 28;
num3 = -25;
return 0;
}
e) Write a C++ statement that stores the average of num1, num2, and
num3, into average.
int main()
{
int sum,num1,num2,num3,average;
num1 = 125;
num2 = 28;
num3 = -25;
sum = num1 + num2 + num3 ;
average = sum / 3;
return 0;
}
f) Write C++ statements that output the values of num1, num2, num3,
and average.
int main()
{
int sum,num1,num2,num3,average;
cout<<"num1= ";
cin>>num1;
cout<<"num2= ";
cin>>num2;
cout<<"num3= ";
cin>>num3;
sum = num1 + num2 + num3 ;
average = sum / 3;
cout<<"sum= "<<sum<<endl;
cout<<"average= "<<average;
return 0;
}
int main()
{
int sum,num1,num2,num3,average;
cout<<"num1= ";
cin>>num1;
cout<<"num2= ";
cin>>num2;
cout<<"num3= ";
cin>>num3;
sum = num1 + num2 + num3 ;
average = sum / 3;
cout<<"sum= "<<sum<<endl;
cout<<"average= "<<average;
return 0;
}
Q5) Consider the following C++ program in which the statements are in the
incorrect order. Rearrange the statements so that it prompts the user to input
the length and width of a rectangle and output the area and perimeter of the
rectangle.
a) Write C++ statements that include the header files iostream and
string.
#include <string>
int main()
return 0;
c) Write C++ statements that declare and initialize the following named constants: SECRET of
type int initialized to 11 and RATE of type double initialized to 12.50.
#include <string>
int main()
int SECRET;
double RATE;
SECRET = 11;
RATE = 12.50;
cout<<"SECRET= "<<SECRET<<endl;
cout<<"RATE= "<<RATE;
return 0;
d) Write C++ statements that declare the following variables: num1, num2,
and newNum of type int; name of type string; and hoursWorked and
wages of type double.
#include <string>
int main()
int num1,num2,newNum;
string name;
double hoursWorked,wages;
return 0;
e) Write C++ statements that prompt the user to input two integers and
store the first number in num1 and the second number in num2.
#include <string>
int main()
{
int num1,num2;
num1=4;
num2=5;
cout<<"num1= "<<num1<<endl;
cout<<"num2= "<<num2;
return 0;
f) Write a C++ statement(s) that outputs the values of num1 and num2,
indicating which is num1 and which is num2. For example, if num1 is 8
and num2 is 5, then the output is:
The value of num1 = 8 and the value of num2 = 5.
#include <string>
int main()
int num1,num2;
num1=6;
num2=4;
return 0;
}
g) Write a C++ statement that multiplies the value of num1 by 2, adds the
value of num2 to it, and then stores the result in newNum. Then, write a
C++ statement that outputs the value of newNum.
#include <string>
int main()
int num1,num2,newNum;
cin>>num1;
cin>>num2;
cout<<"newNum= "<<newNum;
return 0;
h) Write a C++ statement that updates the value of newNum by adding the value of the named
constant SECRET. Then, write a C++ statement that outputs the value of newNum with an
appropriate message.
#include <cmath>
int main()
{
double newNum,SECRET;
cout<<"newNum: ";
cin>>newNum;
cout<<"SECRET: ";
cin>>SECRET;
newNum += SECRET;
return 0;
i) Write C++ statements that prompt the user to enter a person’s last name
and then store the last name into the variable name.
#include <string>
int main()
string lastName;
cin>>lastName;
return 0;
j) Write C++ statements that prompt the user to enter a decimal number
between 0 and 70 and then store the number entered into hoursWorked.
#include <math.h>
int main()
double hoursWorked;
hoursWorked = 40.3;
cout<<"hoursWorked= "<<hoursWorked;
return 0;
k) Write a C++ statement that multiplies the value of the named constant
RATE with the value of hoursWorked and then stores the result into the
variable wages.
#include <cmath>
int main()
{
double RATE,hoursWorked,wages;
cout<<"RATE: ";
cin>>RATE;
cout<<"hoursWorked: ";
cin>>hoursWorked;
cout<<"wages: "<<wages;
return 0;
#include <math.h>
#include <string>
int main()
{
double hoursWorked,payRate,salary;
string name,Rainbow;
cout<<"hoursWorked: ";
cin>>hoursWorked;
cout<<"payRate:$ ";
cin>>payRate;
cout<<"salary:$ ";
cin>>salary;
cout<<"name: ";
cin>>name;
return 0;
m) Write a C++ program that tests each of the C++ statements that you
wrote in parts a through l. Place the statements at the appropriate place
in the previous C++ program segment. Test run your program (twice)
on the following input data:
#include <math.h>
#include <string>
int main()
{
int num1,num2;
string name;
double hoursWorked;
num1 = 13;
num2 = 28;
name = "Jacobson";
hoursWorked = 48.30;
cout<<"num1 = "<<num1<<endl;
cout<<"num2 = "<<num2<<endl;
cout<<"name = "<<name<<endl;
cout<<"hoursWorked = "<<hoursWorked;
return 0;
int main()
int num1,num2;
string name;
double hoursWorked;
num1 = 32;
num2 = 15;
name = "Crawford";
hoursWorked = 58.45 ;
cout<<"num1 = "<<num1<<endl;
cout<<"num2 = "<<num2<<endl;
cout<<"name = "<<name<<endl;
cout<<"hoursWorked = "<<hoursWorked;
return 0;
Q9) Write a program that prompts the user to enter five test scores and then prints
the average test score. (Assume that the test scores are decimal numbers.)
#include <string>
int main()
double t1,t2,t3,t4,t5;
double average,sum;
cout<<"t1= ";
cin>>t1;
cout<<"t2= ";
cin>>t2;
cout<<"t3= ";
cin>>t3;
cout<<"t4= ";
cin>>t4;
cout<<"t5= ";
cin>>t5;
sum = t1 + t2 + t3 + t4 + t5;
average = sum / 5;
cout<<"average= "<<average;
return 0;
Q10) Write a program that prompts the user to input five decimal numbers. The
program should then add the five decimal numbers, convert the sum to the
nearest integer, and print the result.
#include <math.h>
int main()
double n1,n2,n3,n4,n5,a;
cout<<"n1= ";
cin>>n1;
cout<<"n2= ";
cin>>n2;
cout<<"n3= ";
cin>>n3;
cout<<"n4= ";
cin>>n4;
cout<<"n5= ";
cin>>n5;
a = n1 + n2 + n3 + n4 + n5;
cout<<"a= "<<round(a);
return 0;
Q18) A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships
cartons of milk to a local grocery store. The cost of producing one liter of
milk is $0.38, and the profit of each carton of milk is $0.27. Write a
program that does the following:
a) Prompts the user to enter the total amount of milk produced in the
morning.
#include <math.h>
int main()
double cartonHold,costofOneliter,profitofEachcarton,totalamountofMilk;
cartonHold = 3.78;
costofOneliter = 0.38;
profitofEachcarton = 0.27;
cout<<"totalamountofMilk= ";
cin>>totalamountofMilk;
return 0;
b) Outputs the number of milk cartons needed to hold milk. (Round your
answer to the nearest integer.)
#include <math.h>
int main()
double cartonHold,costofOneliter,profitofEachcarton,totalamountofMilk,a;
cartonHold = 3.78;
costofOneliter = 0.38;
profitofEachcarton = 0.27;
cout<<"totalamountofMilk= ";
cin>>totalamountofMilk;
a = totalamountofMilk / cartonHold;
return 0;
#include <math.h>
double cartonHold,costofOneliter,profitofEachcarton,totalamountofMilk,a,b;
cartonHold = 3.78;
costofOneliter = 0.38;
profitofEachcarton = 0.27;
cout<<"totalamountofMilk= ";
cin>>totalamountofMilk;
a = totalamountofMilk / cartonHold;
b = a * costofOneliter;
return 0;
#include <math.h>
int main()
double cartonHold,costofOneliter,profitofEachcarton,totalamountofMilk,a,b,c;
cartonHold = 3.78;
costofOneliter = 0.38;
profitofEachcarton = 0.27;
cout<<"totalamountofMilk= ";
cin>>totalamountofMilk;
a = totalamountofMilk / cartonHold;
b = a * costofOneliter;
c = a * profitofEachcarton;
return 0;
Q20) You found an exciting summer job for five weeks. It pays, say, $15.50
per hour. Suppose that the total tax you pay on your summer job
income is 14%. After paying the taxes, you spend 10% of your net
income to buy new clothes and other accessories for the next school
year and 1% to buy school supplies. After buying clothes and school
supplies, you use 25% of the remaining money to buy savings bonds.
For each dollar you spend to buy savings bonds, your parents spend $0.50 to buy additional
savings bonds for you. Write a program that prompts the user to enter the pay rate for an hour
and the number of hours you worked each week. The program then outputs them following:
a) Your income before and after taxes from your summer job.
{
double payRatehour,workedNumberofhours,beforeTax,afterTax,tax;
tax = 0.14;
cout << "payRatehour: " ;
cin>>payRatehour;
cout<<"workedNumberofhours: ";
cin>>workedNumberofhours;
beforeTax = payRatehour * workedNumberofhours;
afterTax = beforeTax * tax;
cout.precision(2);
cout<<"beforeTax: "<<beforeTax<<endl;
cout<<"afterTax: "<<afterTax;
return 0;
}
{
double
payRatehour,workedNumberofhours,clothesotherAccessories,moneySpendonClothesaccessori
es,beforeTax,afterTax,tax;
tax = 0.14;
clothesotherAccessories = 0.10;
cout << "payRatehour: " ;
cin>>payRatehour;
cout<<"workedNumberofhours: ";
cin>>workedNumberofhours;
beforeTax = payRatehour * workedNumberofhours;
afterTax = beforeTax * tax;
moneySpendonClothesaccessories = afterTax * clothesotherAccessories;
cout.precision(2);
cout<<"beforeTax: "<<beforeTax<<endl;
cout<<"afterTax: "<<afterTax<<endl;
cout<<"moneyspendoneclothesandaccessories: "<<moneySpendonClothesaccessories;
return 0;
}
{
double
payRatehour,workedNumberofhours,clothesotherAccessories,moneySpendonSchool,moneySp
endonClothesaccessories,schoolSupplies,beforeTax,afterTax,tax;
tax = 0.14;
clothesotherAccessories = 0.10;
schoolSupplies = 0.01;
cout << "payRatehour: " ;
cin>>payRatehour;
cout<<"workedNumberofhours: ";
cin>>workedNumberofhours;
beforeTax = payRatehour * workedNumberofhours;
afterTax = beforeTax * tax;
moneySpendonClothesaccessories = afterTax * clothesotherAccessories;
moneySpendonSchool = afterTax * schoolSupplies;
cout.precision(2);
cout<<"beforeTax: "<<beforeTax<<endl;
cout<<"afterTax: "<<afterTax<<endl;
cout<<"moneyspendoneclothesandaccessories: "<<moneySpendonClothesaccessories<<endl;
cout<<"moneySpendonschool: "<<moneySpendonSchool;
return 0;
}
{
double
payRatehour,savingBonds,workedNumberofhours,clothesotherAccessories,moneySpendonSch
ool,moneySpendonClothesaccessories,schoolSupplies,beforeTax,afterTax,tax;
tax = 0.14;
clothesotherAccessories = 0.10;
schoolSupplies = 0.01;
cout << "payRatehour: " ;
cin>>payRatehour;
cout<<"workedNumberofhours: ";
cin>>workedNumberofhours;
beforeTax = payRatehour * workedNumberofhours;
afterTax = beforeTax * tax;
moneySpendonClothesaccessories = afterTax * clothesotherAccessories;
moneySpendonSchool = afterTax * schoolSupplies;
savingBonds = 0.25 * (afterTax - moneySpendonClothesaccessories - moneySpendonSchool);
cout.precision(2);
cout<<"beforeTax: "<<beforeTax<<endl;
cout<<"afterTax: "<<afterTax<<endl;
cout<<"moneyspendoneclothesandaccessories: "<<moneySpendonClothesaccessories<<endl;
cout<<"moneySpendonschool: "<<moneySpendonSchool<<endl;
cout<<"savingBonds: "<<savingBonds;
return 0;
}
e) The money your parents spend to buy additional savings bonds for
you.
{
double
payRatehour,savingBonds,workedNumberofhours,clothesotherAccessories,moneySpendonSch
ool,moneySpendonClothesaccessories,schoolSupplies,beforeTax,afterTax,tax,parentsSpend,add
itionalSavingbonds;
tax = 0.14;
clothesotherAccessories = 0.10;
schoolSupplies = 0.01;
parentsSpend = 0.50;
cout << "payRatehour: " ;
cin>>payRatehour;
cout<<"workedNumberofhours: ";
cin>>workedNumberofhours;
beforeTax = payRatehour * workedNumberofhours;
afterTax = beforeTax * tax;
moneySpendonClothesaccessories = afterTax * clothesotherAccessories;
moneySpendonSchool = afterTax * schoolSupplies;
savingBonds = 0.25 * (afterTax - moneySpendonClothesaccessories - moneySpendonSchool);
additionalSavingbonds = savingBonds * parentsSpend;
cout.precision(2);
cout<<"beforeTax: "<<beforeTax<<endl;
cout<<"afterTax: "<<afterTax<<endl;
cout<<"moneyspendoneclothesandaccessories: "<<moneySpendonClothesaccessories<<endl;
cout<<"moneySpendonschool: "<<moneySpendonSchool<<endl;
cout<<"savingBonds: "<<savingBonds<<endl;
cout<<"additionalSavingbonds: "<<additionalSavingbonds;
return 0;
}
Q23) Newton’s law states that the force, F, between two bodies of masses M1 and
M2 is given by:
F = k(M1M2/d2)
__
;
in which k is the gravitational constant and d is the distance between the
bodies. The value of k is approximately 6.67_10-8 dyn. cm2/g2.
Write a program that prompts the user to input the masses of the bodies and the
distance between the bodies. The program then outputs the force between
the bodies.
return 0;
}
Q24) One metric ton is approximately 2205 pounds. Write a program that
prompts the user to input the amount of rice, in pounds, in a bag. The
program outputs the number of bags needed to store one metric ton of rice.
ANS) #include <iostream>
#include <cmath>
int main()
double rice,oneMetricton,bags;
oneMetricton = 2205;
cout<<"rice: ";
cin>>rice;
cout<<"bags: "<<bags;
return 0;