You are on page 1of 2

Sultan Qaboos University

College of Science - Department of Computer Science


COMP2002

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:

Surface Area= ½ π D2 + π D · Height

Volume = ¼ π D2 · Height

Where π is a constant value equals 3.141593. Define π as symbolic constant.

Your program should display and format the circle radius, area and circumference values as illustrated in the
sample output below.

Sample Output

• Use <iomanip> setprecision(): cout<<fixed<<setprecision(2); or you can use ostream


functions: cout.setf(ios::fixed); cout.precision(2);

• Print units after the values.

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”;).

\t: leave a tab before printing the next output.


\n: go to the beginning of the next line. This is similar to endl

o Use appropriate <cmath> library functions to compute the square, cube and square root values
of a given number. Tip: use pow(), sqrt().

o Format the square root value in 1 decimal place.

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 Format the square root value in 1 decimal place.

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:

Mohammed, you are 1.8923 meter tall.

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

You might also like