Professional Documents
Culture Documents
LAB 3
• Attempt the following lab exercise. You are responsible to finish all the questions.
• Use the 5-step problem solving methodology where applicable.
• Your C++ program should include the following features:
- Meaningful variable names
- Sufficient comments
- Proper indentation
- Reasonable spacing between statements.
1. Write a C++ program named cylinder.cpp that prompts the user for the values of diameter (D) and height
in meters and computes the cylinder surface area and volume using the following equations:
Volume = ¼ π D2 · Height
Your program should display and format the circle radius, area and circumference values as illustrated in the
sample output below.
Sample Output
NB: Test your program with different input data values for the circle radius.
AB 1
2. Write a C++ program named table1.cpp that prompts the user for a positive integer number and
displays the number, its square, its cube and its square root values in a tabular format. For instance, if
the user enters the number 3, your program displays the following output:
Sample Output
o Display table header and the values in tabular format as shown in the sample output using the tab
character '\t' and the new line character '\n' (e.g. cout<<”\tx\tx^2\tx^3\tsqrt(x)\n”;).
o Use appropriate <cmath> library functions to compute the square, cube and square root values
of a given number. Tip: use pow(), sqrt().
3. Rewrite the program in Q2 using <iomanip> library. Name your program table2.cpp.
o Display the output in tabular format as shown in sample 2 output. Use setw() function from
<iomanip> library with appropriate width size. (e.g. cout<<setw(10)<<”x”
<<setw(12)<<”x^2” <<setw(12)<<”x^3” <<setw(15)<<”sqrt(x)\n”;
o Compare the following sample output with the sample output of table1.cpp in Q2.
Sample Output
NB: Test your programs with different input data values for x.
4. Extra practice:
o Write a program height.cpp that asks the user to enter his/her first name and height in inches
(Note: 1 inch = 2.54 cm). Then it display the information in the following format:
o Write a program name.cpp that asks the user for the first name then the last name, and then
prints the names in the format: lastname, firstname. Example: Al-Salmi, Omer.
AB 2