Work Sheet 2 - Basics Programming Constructors
Work Sheet 2 - Basics Programming Constructors
Work Sheet 2
Basics Programming Constructors
1. Which of the following represent valid variable definitions?
Identifier 2by2
seven_11 Default
_unique_ average_weight_of_a_large_pizza
gross-income variable
gross$income object.oriented
▪ Age of a person.
▪ Income of an employee.
▪ Number of words
▪ A greeting message.
6. What will be the value of each of the following variables after its initialization:
double d = 2 * int(3.14);
long k = 3.14 - 3;
7. For each of the following problems write a C++ code to perform the required task.
a) Print sum, difference, product, and quotient of given integers respectively.
b) Find the average of two numbers given by the user
c) Perform date arithmetic, such as how many days there are between 6/1/90 and 8/3/92.
d) Transfer inches to centimeters (1 inch= 2.54 centimeters)
e) Calculate the power of a number (xx)
f) Reads a number n and prints 2n
g) Area and circumference of circle based on its radius entered from the keyboard.
h) Volume and the area of sphere of a radius r accepting its radius from user
i) Rectangle area and circumstance by reading height and width from user.
j) Volume and the area of a cylinder of radius r and height h
8. Compute the value of each legal C++ arithmetic expression. If the expression is not legal,
explain why. Clearly indicate what the data type of the result of each expression would be.
a) (1.0 / 4) * 6
b) (6 * 3) % 5
c) (3 / 4) * 4
d) (4.0 * 2) % 5
e) ((10 + 5 / 2) / 3)
9. Add extra brackets to the following expressions to explicitly show the order in which the
operators are evaluated:
a) (n <= p + q && n >= p - q || n == 0)
b) (++n * q-- / ++p - q)
c) (n | p & q ^ p << 2 + q)
d) (p < q ? n < p ? q * n - 2 : q / n + 1 : q - n)
10. (De Morgan’s Laws) De Morgan’s laws can sometimes make it more convenient for us to
express a logical expression. These laws state that the expression !(condition1 &&
condition2) is logically equivalent to the expression (!condition1 || !condition2). Also, the
expression !(condition1 || condition2) is logically equivalent to the expression (!condition1
&& !condition2). Use De Morgan’s laws to write equivalent expressions for each of the
following:
12. Write a program that converts a temperature given in Fahrenheit into Celsius and vice versa.
13. Write a program that converts a letter entered from the keyboard to its uppercase or lower
case equivalent (Hint :- use the function defined in ctype.h library header file).
14. A serial transmission line can transmit 960 characters a second. Write a program that will
calculate how long it will take to send a file, given the file size. Try it on a 400MB
(419,430,400 byte) file. Use appropriate units. (A 400MB file takes days.)
15. Write a program that prompts the capacity in gallons of an automobile fuel tank and the miles
per gallons the automobiles can be driven. The program outputs the number miles the
automobile cam be driven without refueling.
16. Write a program that swaps the values of two variables and displays their former (previous)
and current values. The values of the variables are to be entered from the keyboard.
17. Make a program that calculates and display the Body Mass Index (BMI) of a person. The
program should read the height and weight of a person being given.
18. Write a c ++ program that prompts the user to enter the elapsed time for an event in hours,
minutes and seconds. The program then outputs the elapsed time in seconds.
19. Write a program that accepts day, month and year in number (DD-MM-YY) and displays for
the user as (MM- DD-YY). For example it accepts 12 09 2017 displays September 12, 2017.
20. John has grown mango tree. He has 80 mango trees over a given area. Currently he gets 900
fruits per tree. If he plants additional mango tree in the same area. The fruit decreased by 4 in
the whole tree. Write a program which shows the number of mango tree increment and their
fruit decrement.
21. Write a C++ program that resolve a force into its X and Y component with a given angle.
22. Write a program to calculate the distance between two points (x1, y1) and (x2, y2). All
numbers and return values should be of type double. Incorporate this method into an
application that enables the user to enter the coordinates of the points.
24. Develop C++ program that used to calculate percentile mark of student score in 8th grade.
25. Write a C++ program to accept a function and find integration or derivation of the function.