Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Lab Exercise (Chapter 4)

Exercise 1.
Print the value 333.546372 in a field width of 15 characters with
precisions of 1, 2, 3, 4 and 5. Right justify the output. What are the
five values that print? What should you do if you want to print Left
justify output?
Tips:
printf(%15.1f\n, 333.546372);
printf(%-15.1f\n, 333.546372);
Exercise 2.
Create a C program with the code as below and run it. Do you able
to identify the error? Explain and correct it.

Exercise 3.
(4.12 Calculating the Sum of Even Integers)
Write a program that calculates and prints the sum of the even
integers from 2 to 30, using a single for loop statement.
Exercise 4.
(4.16 Triangle Printing Program)
Write a program that prints the following patterns separately. Use
for loops to generate the patterns. All asterisks (*) should be
printed by a single printf statement of the form printf (*);
[Hint: the second pattern requires that each line begin with an
appropriate number of blanks. And, you will need two for loops
nested]
First pattern:

Second pattern:

Exercise 5.
(Additional Exercise)
Try out the exercise of printing the total grade, sample code in the
Chapter 4 lecture slide Page 58 61. Observe the result.

Objective of this lab exercise:


1. Let student familiarize with the usage of the for looping statement.
2. Let student familiarize with the usage of the switch statement.

Notes to instructor:
1. Instructor can give further guidelines or hints on Exercise 4.
2. Further exercises can be given if time permits

You might also like