Assignment
Assignment
Instructions: Write hand written answers (Not computer typed). Use a separate page for each
question. Write question number, your roll number and name on each page in your own
handwriting. Take snapshots of your assignment and pictures of code running, make a pdf with
your roll number as file name and submit to GOOGLE classroom by last Friday of 13th week of
this semester by 4pm.
14th July 2023 4pm.
Q1: How C++ program is compiled into an executable? Answer in 4 lines only. Make a flow chart.
Q2: Write a program that inputs three integers from the keyboard and prints the sum, average, product,
smallest and largest of these numbers. The screen dialog should appear as follows: Use correctly sized data
type for sum and product outputs.
Q3: Write a program that inputs a five digit integer, separates the integer into its digit and prints them
separated by three spaces each. For example, if the user types in 42339, the program should print:
4 2 3 3 9
Q4: Develop a C++ program that uses a while statement to determine the gross pay for each of several
employees. The company pays “straight time” for the first 40 hours worked by each employee and pays
“time-and-a-half” for all hours worked in excess of 40 hours. You are given a list of the employees of the
company, the number of hours each employee worked last week and the hourly rate of each employee.
Your program should input this information for each employee and should determine and display the
employee’s gross pay.
Sample Output:
Page 1 of 7
Assignment Computers & Programming (EE-163)
Instructions: Write hand written answers (Not computer typed). Use a separate page for each
question. Write question number, your roll number and name on each page in your own
handwriting. Take snapshots of your assignment and pictures of code running, make a pdf with
your roll number as file name and submit to GOOGLE classroom by last Friday of 13th week of
this semester by 4pm.
14th July 2023 4pm.
Q5: Write a program that ask user to enter an integer number and evaluates its factorial. Your program
should print the output as below,
Enter an integer : 5
5 x 4 x 3 x 2 x 1 = 120
Q6: Write a program that ask user to input the number of elements in a Fibonacci sequence and then
generates a Fibonacci sequence up-to the given number of elements.
(Hint: In Fibonacci sequence, the next element is the sum of two previous values)
Sample Output:
Q7: Write a program that reads three non-zero double values and determines and prints whether they
could represents sides of triangle.
[Hint: a,b and c represents sides of triangle if following criteria is met,
a+b>c
a+c>b
b + c > a] Watch out for equality check in double precision floating point numbers
Sample Output:
Q8: Write a program that reads three non-zero double values and determines and prints whether they
are sides of right triangle. The program should verify the results up to 4 decimal places.
[Hint: Use Pythagoras theorem to determine whether the three sides form right triangle.]
Sample Output:
Page 2 of 7
Q9: Write a program that ask user to input a floating point number and computes exponential of that
number using Taylor series as below,
𝑥 𝑥2 𝑥3
𝑒𝑥 = 1 + + + +⋯
1! 2! 3!
Also, prompt the user for desired accuracy of e (i.e., the number of terms in summation).
Sample Output:
Q10: Write a program that ask user to input angle in radians and computes its sine using Taylor series as
below,
∞
(−1)𝑛
sin(𝑥) = ∑ 𝑥2𝑛+1
(2𝑛 + 1)!
𝑛=0
Also, prompt the user for desired accuracy of sine. (i.e., the number of terms in summation).
Sample Output:
Q11: Write a program that prints any one of the following pattern as shown below,
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********
Page 3 of 7
Q12: A right triangle can have sides that are all integers. A set of three integer values for the sides of a
right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of
the squares of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for
side1, side2 and hypotenuse all no larger than 500. Use a triple-nested for loop that tries all
possibilities.
Q13: Consider the following code: Explain the output and any errors if any
#include <iostream>
using namespace std;
int main()
{
unsigned short n=1000;
cout << "n = " << n << endl;
n *= 10;
cout << "n = " << n << endl;
n *= 10;
cout << "n = " << n << endl;
n *= 10;
cout << "n = " << n << endl;
return 0;
}
The output is
n = 1000
n = 10000
n = 34464
n = 16960
Q.14 Consider the following code and its output. Explain any errors.
#include <iostream>
using namespace std;
int main()
{
float x=1000.0;
cout << "x = " << x << endl;
x *= x; // multiplies n by itself; i.e.,it squares x
cout << "x = " << x << endl;
x *= x; // multiplies n by itself; i.e.,it squares x
cout << "x = " << x << endl;
x *= x; // multiplies n by itself; i.e.,it squares x
cout << "x = " << x << endl;
x *= x; // multiplies n by itself; i.e.,it squares x
cout << "x = " << x << endl;
Page 4 of 7
return 0;
}
x = 1000
x = 1e+06
x = 1e+12
x = 1e+24
x = inf
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a,b,c;
cout << "Enter the coefficients of a quadratic equation:" << endl;
cout << "\ta: ";
cin >> a;
cout << "\tb: ";
cin >> b;
cout << "\tc: ";
cin >> c;
cout << "The equation is: " << a << "*x*x + " << b
<< "*x + " << c << " = 0" << endl;
float d = b*b - 4*a*c; // discriminant
float sqrtd = sqrt(d);
float x1 = (-b + sqrtd)/(2*a);
float x2 = (-b - sqrtd)/(2*a);
cout << "The solutions are:" << endl;
cout << "\tx1 = " << x1 << endl;
cout << "\tx2 = " << x2 << endl;
cout << "Check:" << endl;
cout << "\ta*x1*x1 + b*x1 + c = " << a*x1*x1 + b*x1 + c << endl;
cout << "\ta*x2*x2 + b*x2 + c = " << a*x2*x2 + b*x2 + c << endl;
return 0;
}
Outputs:
Page 5 of 7
The first output is correct, but the second output check is not correct. Explain the error and any corrective
measure if possible.
Q.16 Create a BMI calculator application that reads the user’s weightin pounds and height in inches (or,
if you prefer, the user’s weight in kilograms and height in meters), then calculates and displays the
user’s body mass index. Also, the application should display the following information from the
Department of Health and Human Services/National Institutes of Health so the user can evaluate
his/her BMI:
Or
𝑊𝑒𝑖𝑔ℎ𝑡 𝑖𝑛 𝑘𝑖𝑙𝑜𝑔𝑟𝑎𝑚𝑠
𝐵𝑀𝐼 =
𝐻𝑒𝑖𝑔ℎ𝑡 𝑖𝑛 𝑚𝑒𝑡𝑒𝑟𝑠 × 𝐻𝑒𝑖𝑔ℎ𝑡 𝑖𝑛 𝑚𝑒𝑡𝑒𝑟𝑠
Page 6 of 7
BMI VALUES
Underweight: less than 18.5
Normal: between 18.5 and 24.9
Overweight: between 25 and 29.9
Obese: 30 or greater
Q.17 (Printing the Decimal Equivalent of a Binary Number) Input an integer containing only 0s and 1s (i.e., a
“binary” integer) and print its decimal equivalent. Use the remainder and division operators to pick off
the “binary” number’s digits one at a time from right to left.
Q.18 Print a table that shows the value of PI approximated by computing the first 200,000 terms in this series. How
many terms do you have to use before you first get a value that begins with 3.14159?
𝟒 𝟒 𝟒 𝟒 𝟒
𝝅=𝟒− + − + − +⋯
𝟑 𝟓 𝟕 𝟗 𝟏𝟏
Q.19 Write a code which generates prime numbers in the quantity as specified by the user.
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, and
227
Q.20 Write half page summary of any one of the following case studies from the chapter 6 of the book,
titled Does Not Compute Humble Pi, A comedy of math errors by Matt Parker. Your summary should
include a brief introduction of the problem, what errors it caused and any ways to rectify the problem.
a) Civilizations Game
b) Switzerland Trains
c) Deadly code: Therac 25
d) MS Excel problem
e) Dangers of Truncation
f) Divide by Zero
Page 7 of 7