Floyd’s triangle in PL/SQL

Prerequisite PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.

Floyd’s triangle is a right-angled triangular array of natural numbers. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

Given a range of number and the task is to form Floyd’s triangle.
Examples:



Input: 1-29
Output:
 1
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20 21
 22 23 24 25 26 27 28

Below is the required implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

   
--floyd's triangle in PL/SQL 
DECLARE 
    --num, var_num variable declare 
    --num assign 1 
    num     NUMBER := 1; 
    var_num VARCHAR2(200); 
BEGIN 
    --loop  from 1 to 16 
    FOR i IN 1..16 LOOP 
        FOR j IN 1..i LOOP 
            var_num := var_num 
                       ||' ' 
                       ||num; 
  
            num := num + 1; 
  
            exit WHEN num = 16; 
        END LOOP; 
  
        --result print 
        dbms_output.Put_line(var_num); 
  
        exit WHEN num = 16; 
  
        var_num := NULL
    END LOOP; 
--end lop 
END
--end program 

chevron_right


Output:

 1
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.