You are on page 1of 4

C Programming Lab

Universiti Tenaga Nasional




5 Repetition Structure

Sample 1

Create a system that will accept even numbers. The system will only stop when the user
enters an odd number. Based on the algorithm below, write the full program.

Pseudocode

begin
read number
while number % 2 == 0
read number
end while
end

Flowchart







begin
read number
number
%2 == 0
read number
end
n
y
C Programming Lab
Universiti Tenaga Nasional

Sample 2

Write a program that reads two positive integers corresponding to two year values, ensures
that the first year value is less than the second, and then determines and outputs all year
values for leap years. A leap year is one that can be evenly divided by 4, unless it is a
centennial, in which case it must be evenly divided by 400. For example, 1600 and 1992 are
leap years, whereas 1700 and 1998 are not. (if you are still not clear, you can go to
http://www.dpbsmith.com/leapyearfaq.txt. Based on the algorithm below, write the full
program.

Pseudocode

begin
read year1, year2
while year1 < year2
if year1 %4 == 0
if year1 % 100 == 0
if year1 % 400 == 0
print year1
end if
else
print year1
end if
end if
year1++
end while
end

Flowchart







begin
print yr1
yr1<=yr2
read yr1, yr2
end
n
y
yr1%4==0
yr1%100
== 0
yr1%400
== 0
n
print yr1
y
y
y
n
n
yr1++
C Programming Lab
Universiti Tenaga Nasional

Question 1

Modify the program in Sample 2 to make it able to ensure that the first year entered should be
smaller than the second year.

Question 2

Write a program that computes and displays the sum of a collection of Celsius temperatures
entered at until a value of -275 is entered.

Question 3

Write a program to process weekly employee time cards for 5 employees of an organization.
Each employee will have three data items: an identification number, the hourly wage rate and
the number of hours worked during a given week. Each employee is to be paid time and a
half for all hours worked over 40. A tax amount of 3.625 % of gross salary will be deducted.
The program output should show the employees number and net pay. Display the total
payroll and the average paid at the end of the run.

Question 4

Write a program that reports the contents of a compressed-gas cylinder based on the first
letter of the cylinders colour. The program input is a character representing the observed
colour of the cylinder: Y or y for yellow, O or o for orange and so on. Cylinder colours and
associated contents are as follows:

Orange Ammonia
Brown Carbon
monoxide
Yellow Hydrogen
Green Oxygen

However to start the program, the user needs to enter a password (for example: 111), and
once the user enter an unknown character, the system will end.
Example output is as shown below:




C Programming Lab
Universiti Tenaga Nasional

Question 5

Create a system the will produce output as shown. You need to use nested for loop.


Question 6

Modify the coding in Question 6, to get this output

You might also like