You are on page 1of 2

Exercise 3

1. Write a PL/SQL block to accept a year and check whether it is a leap year. For example, if the
year entered is 1990, the output should be “1990 is not a leap year.”

DECLARE
v_YEAR NUMBER(4) := '1990';
v_REMAINDER1 NUMBER(5,2);
v_REMAINDER2 NUMBER(5,2);
v_REMAINDER3 NUMBER(5,2);

BEGIN
v_REMAINDER1 := MOD(v_YEAR,4);
v_REMAINDER2 := MOD(v_YEAR,100);
v_REMAINDER3 := MOD(v_YEAR,400);
IF ((v_REMAINDER1 = 0 AND v_REMAINDER2 <> 0 ) OR v_REMAINDER3 = 0)
THEN DBMS_OUTPUT.PUT_LINE(v_YEAR || ' is a leap year');

ELSE DBMS_OUTPUT.PUT_LINE(v_YEAR || ' is not a leap year');


END IF;

END;

2. Write a PL/SQL block to display the country_id and country_name values from the COUNTRIES
table for country_id whose values range from 51 through 55. Use a WHILE loop. Increment a
variable from 51 through 55. Test your variable to see when it reaches 55. EXIT the loop after
you have displayed the 5 countries.

3. Write a PL/SQL block to display the country_id and country_name values from the COUNTRIES
table for country_id whose values range from 51 through 55 in the reverse order. Use a FOR
loop.

4. Write and execute an anonymous block that declares and populates an INDEX BY table of
countries in South America (region_id = 5). The table should use country_id as a primary key,
and should store the country names as the element values. The data should be stored in the
table in ascending sequence of country_id. The block should not display any output. Save your
code.

5. Modify the block so that after populating the INDEX BY table, it uses a FOR loop to display the
contents of the INDEX BY table. You will need to use the FIRST, LAST, and EXISTS table methods.
Execute the block and check the displayed results. Save your code.
6. Modify the block again so that instead of displaying all the contents of the table, it displays only
the first and last elements and the number of elements in the INDEX BY table. Execute the block
and check the displayed results.
7. Upload your script on a Word Document with the filename Exer3_LastName.docx.

You might also like